View Javadoc
1   /*
2    * JavaBean Tester (https://github.com/hazendaz/javabean-tester)
3    *
4    * Copyright 2012-2021 Hazendaz.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * http://www.apache.org/licenses/LICENSE-2.0.txt
10   *
11   * Contributors:
12   *     CodeBox (Rob Dawson).
13   *     Hazendaz (Jeremy Landis).
14   */
15  package com.codebox.instance;
16  
17  import java.lang.reflect.InvocationTargetException;
18  
19  import mockit.Expectations;
20  import mockit.Mocked;
21  import mockit.Tested;
22  
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.api.Disabled;
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * The Class ClassInstanceTest.
29   */
30  // TODO 1/12/2019 JWL Class is not mockable
31  @Disabled
32  class ClassInstanceTest {
33  
34      /** The class instance. */
35      @Tested
36      ClassInstance<Object> classInstance;
37  
38      /** The mock clazz. */
39      @Mocked
40      Class<Object> mockClazz;
41  
42      /**
43       * New instance instantiation exception.
44       *
45       * @throws InstantiationException
46       *             the instantiation exception
47       * @throws IllegalAccessException
48       *             the illegal access exception
49       * @throws NoSuchMethodException
50       *             the no such method exception
51       * @throws InvocationTargetException
52       *             the invocation target exception
53       */
54      @Test
55      void newInstanceInstantiationException()
56              throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
57          Assertions.assertNotNull(new Expectations() {
58              {
59                  ClassInstanceTest.this.mockClazz.getDeclaredConstructor().newInstance();
60                  this.result = new InstantiationException();
61              }
62          });
63          this.classInstance.newInstance(this.mockClazz);
64      }
65  
66      /**
67       * New instance illegal access exception.
68       *
69       * @throws InstantiationException
70       *             the instantiation exception
71       * @throws IllegalAccessException
72       *             the illegal access exception
73       * @throws NoSuchMethodException
74       *             the no such method exception
75       * @throws InvocationTargetException
76       *             the invocation target exception
77       */
78      @Test
79      void newInstanceIllegalAccessException()
80              throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
81          Assertions.assertNotNull(new Expectations() {
82              {
83                  ClassInstanceTest.this.mockClazz.getDeclaredConstructor().newInstance();
84                  this.result = new IllegalAccessException();
85              }
86          });
87          this.classInstance.newInstance(this.mockClazz);
88      }
89  
90  }