1
2
3
4
5
6
7
8
9 package com.codebox.instance;
10
11 import java.lang.reflect.Constructor;
12 import java.lang.reflect.InvocationTargetException;
13
14 import org.junit.jupiter.api.Assertions;
15 import org.junit.jupiter.api.Disabled;
16 import org.junit.jupiter.api.Test;
17 import org.junit.jupiter.api.extension.ExtendWith;
18 import org.mockito.InjectMocks;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.junit.jupiter.MockitoExtension;
22
23
24
25
26
27 @Disabled
28 @ExtendWith(MockitoExtension.class)
29 class ConstructorInstanceTest {
30
31
32 @InjectMocks
33 ConstructorInstance constructorInstance;
34
35 @Mock
36 Constructor<?> mockConstructor;
37
38
39
40
41
42
43
44
45
46
47
48 @Test
49 void newInstanceInstantiationException()
50 throws InstantiationException, IllegalAccessException, InvocationTargetException {
51 Mockito.when(mockConstructor.newInstance()).thenThrow(new InstantiationException());
52
53 Assertions.assertThrows(InstantiationException.class, () -> ConstructorInstance.newInstance(mockConstructor));
54 }
55
56
57
58
59
60
61
62
63
64
65
66 @Test
67 void newInstanceIllegalAccessException()
68 throws InstantiationException, IllegalAccessException, InvocationTargetException {
69 Mockito.when(mockConstructor.newInstance()).thenThrow(new IllegalAccessException());
70
71 Assertions.assertThrows(IllegalAccessException.class, () -> ConstructorInstance.newInstance(mockConstructor));
72 }
73
74
75
76
77
78
79
80
81
82
83
84 @Test
85 void newInstanceInvocationTargetException()
86 throws InstantiationException, IllegalAccessException, InvocationTargetException {
87 Mockito.when(mockConstructor.newInstance()).thenThrow(new InvocationTargetException(new Exception()));
88
89 Assertions.assertThrows(InvocationTargetException.class,
90 () -> ConstructorInstance.newInstance(mockConstructor));
91 }
92
93 }