1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4
5 import mockit.internal.expectations.invocation.MissingInvocation;
6 import mockit.internal.expectations.invocation.UnexpectedInvocation;
7
8 import org.junit.Rule;
9 import org.junit.Test;
10 import org.junit.rules.ExpectedException;
11
12
13
14
15 public final class VerificationsTest {
16
17
18 @Rule
19 public final ExpectedException thrown = ExpectedException.none();
20
21
22
23
24 public static class Dependency {
25
26
27
28
29
30
31
32 public void setSomething(@SuppressWarnings("unused") int value) {
33 }
34
35
36
37
38
39
40
41 public void setSomethingElse(@SuppressWarnings("unused") String value) {
42 }
43
44
45
46
47 public void editABunchMoreStuff() {
48 }
49
50
51
52
53 public void notifyBeforeSave() {
54 }
55
56
57
58
59 public void prepare() {
60 }
61
62
63
64
65 public void save() {
66 }
67 }
68
69
70 @Mocked
71 Dependency mock;
72
73
74
75
76 void exerciseCodeUnderTest() {
77 mock.prepare();
78 mock.setSomething(123);
79 mock.setSomethingElse("anotherValue");
80 mock.setSomething(45);
81 mock.editABunchMoreStuff();
82 mock.notifyBeforeSave();
83 mock.save();
84 }
85
86
87
88
89 @Test
90 public void verifySimpleInvocations() {
91 exerciseCodeUnderTest();
92
93 new Verifications() {
94 {
95 mock.prepare();
96 times = 1;
97 mock.editABunchMoreStuff();
98 mock.setSomething(45);
99 }
100 };
101 }
102
103
104
105
106 @Test
107 public void verifyUnrecordedInvocationThatNeverHappens() {
108 thrown.expect(MissingInvocation.class);
109 thrown.expectMessage("45");
110
111 mock.setSomething(123);
112 mock.prepare();
113
114 new Verifications() {
115 {
116 mock.setSomething(45);
117 }
118 };
119 }
120
121
122
123
124 @Test
125 public void verifyRecordedInvocationThatNeverHappens() {
126 thrown.expect(MissingInvocation.class);
127
128 new Expectations() {
129 {
130 mock.editABunchMoreStuff();
131 }
132 };
133
134 mock.setSomething(123);
135 mock.prepare();
136
137 new Verifications() {
138 {
139 mock.editABunchMoreStuff();
140 }
141 };
142 }
143
144
145
146
147 @Test
148 public void verifyInvocationThatIsAllowedToHappenOnceOrMoreAndHappensOnce() {
149 mock.prepare();
150 mock.setSomething(123);
151 mock.save();
152
153 new Verifications() {
154 {
155 mock.setSomething(anyInt);
156 mock.save();
157 }
158 };
159 }
160
161
162
163
164 @Test
165 public void verifyUnrecordedInvocationThatShouldHappenButDoesNot() {
166 thrown.expect(MissingInvocation.class);
167
168 mock.setSomething(1);
169
170 new Verifications() {
171 {
172 mock.notifyBeforeSave();
173 }
174 };
175 }
176
177
178
179
180 @Test
181 public void verifyInvocationsWithInvocationCount() {
182 mock.setSomething(3);
183 mock.save();
184 mock.setSomethingElse("test");
185 mock.save();
186
187 new Verifications() {
188 {
189 mock.save();
190 times = 2;
191 }
192 };
193 }
194
195
196
197
198 @Test
199 public void verifyInvocationsWithInvocationCountLargerThanOccurred() {
200 thrown.expect(MissingInvocation.class);
201 thrown.expectMessage("Missing 2 invocations");
202 thrown.expectMessage("any int");
203
204 mock.setSomethingElse("test");
205 mock.setSomething(3);
206 mock.save();
207
208 new Verifications() {
209 {
210 mock.setSomething(anyInt);
211 times = 3;
212 }
213 };
214 }
215
216
217
218
219 @Test
220 public void verifyInvocationsWithInvocationCountSmallerThanOccurred() {
221 thrown.expect(UnexpectedInvocation.class);
222 thrown.expectMessage("1 unexpected invocation");
223 thrown.expectMessage("5");
224
225 mock.setSomethingElse("test");
226 mock.setSomething(3);
227 mock.save();
228 mock.setSomething(5);
229
230 new Verifications() {
231 {
232 mock.setSomething(anyInt);
233 times = 1;
234 }
235 };
236 }
237
238
239
240
241 @Test
242 public void verifyInvocationThatShouldNotOccurButDid() {
243 thrown.expect(UnexpectedInvocation.class);
244 thrown.expectMessage("2 unexpected invocations");
245 thrown.expectMessage("123");
246
247 mock.setSomething(5);
248 mock.setSomething(123);
249
250 new Verifications() {
251 {
252 mock.setSomething(anyInt);
253 maxTimes = 0;
254 }
255 };
256 }
257
258
259
260
261 @Test
262 public void verifyWithArgumentMatcher() {
263 exerciseCodeUnderTest();
264
265 new Verifications() {
266 {
267 mock.setSomething(anyInt);
268 }
269 };
270 }
271
272
273
274
275 @Test
276 public void verifyWithArgumentMatcherAndIndividualInvocationCounts() {
277 exerciseCodeUnderTest();
278
279 new Verifications() {
280 {
281 mock.prepare();
282 maxTimes = 1;
283 mock.setSomething(anyInt);
284 minTimes = 2;
285 mock.editABunchMoreStuff();
286 maxTimes = 5;
287 mock.save();
288 times = 1;
289 }
290 };
291 }
292
293
294
295
296 @Test
297 public void verifyWithCustomArgumentMatcherWithoutArgumentValue() {
298 mock.setSomethingElse("not empty");
299
300 new Verifications() {
301 {
302 mock.setSomethingElse(with(new Delegate<String>() {
303 @Mock
304 boolean isNotEmpty(String s) {
305 return !s.isEmpty();
306 }
307 }));
308 }
309 };
310 }
311
312
313
314
315 @Test
316 public void verifyThroughCapturedArguments() {
317 thrown.expect(AssertionError.class);
318 thrown.expectMessage("not empty");
319
320 mock.setSomethingElse("test");
321
322 new Verifications() {
323 {
324 String value;
325 mock.setSomethingElse(value = withCapture());
326
327 assertEquals(0, value.length(), "not empty");
328 }
329 };
330 }
331
332
333
334
335 @Test
336 public void verifyWithCustomArgumentMatcher() {
337 thrown.expect(MissingInvocation.class);
338 thrown.expectMessage("isEmpty(\"test\")");
339
340 mock.setSomethingElse("test");
341
342 new Verifications() {
343 {
344 mock.setSomethingElse(with(new Delegate<String>() {
345 @Mock
346 boolean isEmpty(String s) {
347 return s.isEmpty();
348 }
349 }));
350 }
351 };
352 }
353
354
355
356
357 @Test
358 public void verifyInvocationThatMatchesExpectationRecordedWithAnyMatcherButWithArgumentValueWhichDidNotOccur() {
359 thrown.expect(MissingInvocation.class);
360 thrown.expectMessage("45");
361
362 new Expectations() {
363 {
364 mock.setSomething(anyInt);
365 }
366 };
367
368 mock.setSomething(123);
369
370 new Verifications() {
371 {
372 mock.setSomething(45);
373 }
374 };
375 }
376
377
378
379
380
381
382
383 @Test
384 public void verityTwoInvocationsToMethodMatchedOnSpecificInstanceWithNoArgumentMatchers(
385 @Injectable final Dependency dep) {
386 dep.editABunchMoreStuff();
387 dep.editABunchMoreStuff();
388
389 new Verifications() {
390 {
391 dep.editABunchMoreStuff();
392 times = 2;
393 }
394 };
395 }
396 }