1
2
3
4
5
6
7
8
9
10
11
12
13
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
30
31
32 @Disabled
33 class ConstructorInstanceTest {
34
35
36 @Tested
37 ConstructorInstance constructorInstance;
38
39
40
41
42
43
44
45
46
47
48
49
50
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
69
70
71
72
73
74
75
76
77
78
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
97
98
99
100
101
102
103
104
105
106
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 }