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.Constructor;
18  import java.lang.reflect.InvocationTargetException;
19  
20  import mockit.Expectations;
21  import mockit.Mocked;
22  import mockit.Tested;
23  
24  import org.junit.jupiter.api.Assertions;
25  import org.junit.jupiter.api.Disabled;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * The Class ConstructorInstanceTest.
30   */
31  // TODO 1/12/2019 JWL Class is not mockable
32  @Disabled
33  class ConstructorInstanceTest {
34  
35      /** The constructor instance. */
36      @Tested
37      ConstructorInstance constructorInstance;
38  
39      /**
40       * New instance instantiation exception.
41       *
42       * @param mockConstructor
43       *            the mock constructor
44       *
45       * @throws InstantiationException
46       *             the instantiation exception
47       * @throws IllegalAccessException
48       *             the illegal access exception
49       * @throws InvocationTargetException
50       *             the invocation target exception
51       */
52      @Test
53      void newInstanceInstantiationException(@Mocked final Constructor<?> mockConstructor)
54              throws InstantiationException, IllegalAccessException, InvocationTargetException {
55          Assertions.assertNotNull(new Expectations() {
56              {
57                  mockConstructor.newInstance();
58                  this.result = new InstantiationException();
59              }
60          });
61  
62          Assertions.assertThrows(InstantiationException.class, () -> {
63              ConstructorInstance.newInstance(mockConstructor);
64          });
65      }
66  
67      /**
68       * New instance illegal access exception.
69       *
70       * @param mockConstructor
71       *            the mock constructor
72       *
73       * @throws InstantiationException
74       *             the instantiation exception
75       * @throws IllegalAccessException
76       *             the illegal access exception
77       * @throws InvocationTargetException
78       *             the invocation target exception
79       */
80      @Test
81      void newInstanceIllegalAccessException(@Mocked final Constructor<?> mockConstructor)
82              throws InstantiationException, IllegalAccessException, InvocationTargetException {
83          Assertions.assertNotNull(new Expectations() {
84              {
85                  mockConstructor.newInstance();
86                  this.result = new IllegalAccessException();
87              }
88          });
89  
90          Assertions.assertThrows(IllegalAccessException.class, () -> {
91              ConstructorInstance.newInstance(mockConstructor);
92          });
93      }
94  
95      /**
96       * New instance invocation target exception.
97       *
98       * @param mockConstructor
99       *            the mock constructor
100      *
101      * @throws InstantiationException
102      *             the instantiation exception
103      * @throws IllegalAccessException
104      *             the illegal access exception
105      * @throws InvocationTargetException
106      *             the invocation target exception
107      */
108     @Test
109     void newInstanceInvocationTargetException(@Mocked final Constructor<?> mockConstructor)
110             throws InstantiationException, IllegalAccessException, InvocationTargetException {
111         Assertions.assertNotNull(new Expectations() {
112             {
113                 mockConstructor.newInstance();
114                 this.result = new InvocationTargetException(this.withInstanceOf(Exception.class));
115             }
116         });
117 
118         Assertions.assertThrows(InvocationTargetException.class, () -> {
119             ConstructorInstance.newInstance(mockConstructor);
120         });
121     }
122 
123 }