1
2
3
4
5
6 package mockit;
7
8 import static org.junit.jupiter.api.Assertions.assertEquals;
9 import static org.junit.jupiter.api.Assertions.fail;
10
11 import mockit.integration.junit5.JMockitExtension;
12
13 import org.junit.jupiter.api.BeforeEach;
14 import org.junit.jupiter.api.Test;
15 import org.junit.jupiter.api.extension.ExtendWith;
16
17
18
19
20 @ExtendWith(JMockitExtension.class)
21 class FinalMockFieldsTest {
22
23
24
25
26 static final class Collaborator {
27
28
29
30
31 Collaborator() {
32 }
33
34
35
36
37
38
39
40 Collaborator(boolean b) {
41 if (!b) {
42 throw new IllegalArgumentException();
43 }
44 }
45
46
47
48
49
50
51 int getValue() {
52 return -1;
53 }
54
55
56
57
58 void doSomething() {
59 }
60 }
61
62
63
64
65 static final class AnotherCollaborator {
66
67
68
69
70
71
72 int getValue() {
73 return -1;
74 }
75
76
77
78
79 void doSomething() {
80 }
81 }
82
83
84 @Injectable
85 final Collaborator mock = new Collaborator();
86
87
88 @Mocked
89 final AnotherCollaborator mock2 = new AnotherCollaborator();
90
91
92
93
94 @BeforeEach
95 void useMockedTypes() {
96 assertEquals(0, mock.getValue());
97 assertEquals(0, mock2.getValue());
98 assertEquals(0, YetAnotherCollaborator.doSomethingStatic());
99 }
100
101
102
103
104 @Test
105 void recordExpectationsOnInjectableFinalMockField() {
106 new Expectations() {
107 {
108 mock.getValue();
109 result = 12;
110 mock.doSomething();
111 times = 0;
112 }
113 };
114
115 assertEquals(12, mock.getValue());
116 }
117
118
119
120
121 @Test
122 void recordExpectationsOnFinalMockField() {
123 AnotherCollaborator collaborator = new AnotherCollaborator();
124
125 new Expectations() {
126 {
127 mock2.doSomething();
128 times = 1;
129 }
130 };
131
132 collaborator.doSomething();
133 assertEquals(0, collaborator.getValue());
134 }
135
136
137 @Mocked
138 final ProcessBuilder mockProcessBuilder = null;
139
140
141
142
143 @Test
144 void recordExpectationsOnConstructorOfFinalMockField() {
145 new Expectations() {
146 {
147 new ProcessBuilder("test");
148 times = 1;
149 }
150 };
151
152 new ProcessBuilder("test");
153 }
154
155
156
157
158 static final class YetAnotherCollaborator {
159
160
161
162
163
164
165
166 YetAnotherCollaborator(boolean b) {
167 if (!b) {
168 throw new IllegalArgumentException();
169 }
170 }
171
172
173
174
175
176
177 int getValue() {
178 return -1;
179 }
180
181
182
183
184 void doSomething() {
185 }
186
187
188
189
190
191
192 static int doSomethingStatic() {
193 return -2;
194 }
195 }
196
197
198 @Mocked
199 final YetAnotherCollaborator unused = null;
200
201
202
203
204 @Test
205 void recordExpectationsOnStaticMethodAndConstructorOfFinalLocalMockField() {
206 new Expectations() {
207 {
208 new YetAnotherCollaborator(true);
209 result = new RuntimeException();
210 YetAnotherCollaborator.doSomethingStatic();
211 result = 123;
212 }
213 };
214
215 try {
216 new YetAnotherCollaborator(true);
217 fail();
218 } catch (RuntimeException ignore) {
219 }
220
221 assertEquals(123, YetAnotherCollaborator.doSomethingStatic());
222 }
223 }