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 org.junit.jupiter.api.Assertions;
21 import org.junit.jupiter.api.Disabled;
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.api.extension.ExtendWith;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.junit.jupiter.MockitoExtension;
28
29
30
31
32
33 @Disabled
34 @ExtendWith(MockitoExtension.class)
35 class ConstructorInstanceTest {
36
37
38 @InjectMocks
39 ConstructorInstance constructorInstance;
40
41 @Mock
42 Constructor<?> mockConstructor;
43
44
45
46
47
48
49
50
51
52
53
54 @Test
55 void newInstanceInstantiationException()
56 throws InstantiationException, IllegalAccessException, InvocationTargetException {
57 Mockito.when(mockConstructor.newInstance()).thenThrow(new InstantiationException());
58
59 Assertions.assertThrows(InstantiationException.class, () -> ConstructorInstance.newInstance(mockConstructor));
60 }
61
62
63
64
65
66
67
68
69
70
71
72 @Test
73 void newInstanceIllegalAccessException()
74 throws InstantiationException, IllegalAccessException, InvocationTargetException {
75 Mockito.when(mockConstructor.newInstance()).thenThrow(new IllegalAccessException());
76
77 Assertions.assertThrows(IllegalAccessException.class, () -> ConstructorInstance.newInstance(mockConstructor));
78 }
79
80
81
82
83
84
85
86
87
88
89
90 @Test
91 void newInstanceInvocationTargetException()
92 throws InstantiationException, IllegalAccessException, InvocationTargetException {
93 Mockito.when(mockConstructor.newInstance()).thenThrow(new InvocationTargetException(new Exception()));
94
95 Assertions.assertThrows(InvocationTargetException.class,
96 () -> ConstructorInstance.newInstance(mockConstructor));
97 }
98
99 }