1
2
3
4
5
6 package mockit;
7
8 import static org.junit.jupiter.api.Assertions.assertThrows;
9 import static org.junit.jupiter.api.Assertions.assertTrue;
10 import static org.junit.jupiter.api.Assertions.fail;
11
12 import java.util.concurrent.Callable;
13
14 import mockit.integration.junit5.JMockitExtension;
15 import mockit.internal.expectations.invocation.MissingInvocation;
16 import mockit.internal.expectations.invocation.UnexpectedInvocation;
17
18 import org.junit.jupiter.api.Test;
19 import org.junit.jupiter.api.extension.ExtendWith;
20
21
22
23
24 @ExtendWith(JMockitExtension.class)
25 class RestrictedFullVerificationsTest {
26
27
28
29
30 @SuppressWarnings("UnusedReturnValue")
31 static class Dependency {
32
33
34
35
36
37
38
39 public void setSomething(@SuppressWarnings("unused") int value) {
40 }
41
42
43
44
45
46
47 public int editABunchMoreStuff() {
48 return 0;
49 }
50
51
52
53
54
55
56 public boolean prepare() {
57 return false;
58 }
59
60
61
62
63 public void save() {
64 }
65
66
67
68
69
70
71
72 static void staticMethod(@SuppressWarnings("unused") String s) {
73 }
74 }
75
76
77
78
79 static final class SubDependency extends Dependency {
80
81
82
83
84
85 int getValue() {
86 return 5;
87 }
88 }
89
90
91
92
93 static final class AnotherDependency {
94
95
96
97
98 void doSomething() {
99 }
100
101
102
103
104
105
106
107
108
109 String doSomethingElse(int i) {
110 return String.valueOf(i);
111 }
112
113
114
115
116
117
118 static boolean staticMethod() {
119 return true;
120 }
121 }
122
123
124 @Mocked
125 Dependency mock;
126
127
128
129
130 void exerciseCodeUnderTest() {
131 mock.prepare();
132 mock.setSomething(123);
133 mock.editABunchMoreStuff();
134 mock.save();
135 }
136
137
138
139
140
141
142
143 @Test
144 void verifyAllInvocationsToOnlyOneOfTwoMockedTypes(@Mocked AnotherDependency mock2) {
145 exerciseCodeUnderTest();
146 mock2.doSomething();
147
148 new FullVerifications(mock) {
149 {
150 mock.prepare();
151 mock.setSomething(anyInt);
152 minTimes = 1;
153 maxTimes = 2;
154 mock.editABunchMoreStuff();
155 mock.save();
156 times = 1;
157 }
158 };
159
160 new FullVerifications(mock.getClass()) {
161 {
162 mock.prepare();
163 mock.setSomething(anyInt);
164 minTimes = 1;
165 maxTimes = 2;
166 mock.editABunchMoreStuff();
167 mock.save();
168 times = 1;
169 }
170 };
171 }
172
173
174
175
176
177
178
179 @Test
180 void verifyAllInvocationsWithSomeMissing(@Mocked final AnotherDependency mock2) {
181 assertThrows(UnexpectedInvocation.class, () -> {
182 exerciseCodeUnderTest();
183 mock2.doSomething();
184
185 new FullVerifications(mock, mock2) {
186 {
187 mock.prepare();
188 mock.setSomething(anyInt);
189 mock.save();
190 mock2.doSomething();
191 }
192 };
193 });
194 }
195
196
197
198
199
200
201
202
203
204
205 @Test
206 void verifyOnlyInvocationsToGenericType(@Mocked final Callable<Dependency> mock2) throws Exception {
207 exerciseCodeUnderTest();
208 mock2.call();
209
210 new FullVerifications(mock2) {
211 {
212 mock2.call();
213 }
214 };
215 }
216
217
218
219
220
221
222
223 @Test
224 void verifyAllInvocationsToInheritedMethods(@Mocked SubDependency mock2) {
225 mock.prepare();
226 mock2.getValue();
227
228 new FullVerifications(mock) {
229 {
230 mock.prepare();
231 }
232 };
233 new FullVerifications(Dependency.class) {
234 {
235 mock.prepare();
236 }
237 };
238 }
239
240
241
242
243
244
245
246 @Test
247 void verifyAllInvocationsToInheritedMethods_whenNotVerified(@Mocked final SubDependency mock2) {
248 assertThrows(UnexpectedInvocation.class, () -> {
249 mock2.prepare();
250 mock2.getValue();
251
252 new FullVerifications(mock2) {
253 {
254 mock2.getValue();
255 }
256 };
257 });
258 }
259
260
261
262
263
264
265
266 @Test
267 void verifyAllInvocationsToSubclassMethods(@Mocked final SubDependency mock2) {
268 mock.prepare();
269 mock2.getValue();
270
271 new FullVerifications(mock2.getClass()) {
272 {
273 mock2.getValue();
274 }
275 };
276 }
277
278
279
280
281
282
283
284 @Test
285 void verifyAllInvocationsToSubclassMethods_whenNotVerified(@Mocked SubDependency mock2) {
286 assertThrows(UnexpectedInvocation.class, () -> {
287 mock.prepare();
288 mock2.getValue();
289
290 new FullVerifications(mock2.getClass()) {
291 {
292 mock.prepare();
293 }
294 };
295 });
296 }
297
298
299
300
301
302
303
304 @Test
305 void verifyAllInvocationsToMethodsOfBaseClassAndOfSubclass(@Mocked final SubDependency mock2) {
306 mock2.prepare();
307 mock2.getValue();
308
309 new FullVerifications(mock2) {
310 {
311 mock2.prepare();
312 mock2.getValue();
313 }
314 };
315 }
316
317
318
319
320
321
322
323 @Test
324 void verifyAllInvocationsToMethodsOfBaseClassAndOfSubclass_whenInheritedMethodNotVerified(
325 @Mocked final SubDependency mock2) {
326 assertThrows(UnexpectedInvocation.class, () -> {
327 mock2.prepare();
328 mock2.getValue();
329
330 new FullVerifications(mock2) {
331 {
332 mock2.getValue();
333 }
334 };
335 });
336 }
337
338
339
340
341
342
343
344 @Test
345 void verifyAllInvocationsToMethodsOfBaseClassAndOfSubclass_whenSubclassMethodNotVerified(
346 @Mocked SubDependency mock2) {
347 assertThrows(UnexpectedInvocation.class, () -> {
348 mock.prepare();
349 mock2.getValue();
350
351 new FullVerifications(mock2) {
352 {
353 mock.prepare();
354 }
355 };
356 });
357 }
358
359
360
361
362 @Test
363 void verifyAllInvocationsWithReplayOnDifferentInstance() {
364 new Dependency().save();
365
366 new FullVerifications(mock) {
367 {
368 new Dependency();
369 mock.save();
370 }
371 };
372 }
373
374
375
376
377
378
379
380 @Test
381 void verifyAllInvocationsWithReplayOnSameInstance(@Mocked final Dependency mock2) {
382 mock2.editABunchMoreStuff();
383
384 new FullVerifications(mock2) {
385 {
386 mock2.editABunchMoreStuff();
387 }
388 };
389 }
390
391
392
393
394
395
396
397 @Test
398 void verifyAllWithReplayOnDifferentInstanceWhenShouldBeSame(@Mocked Dependency mock2) {
399 assertThrows(MissingInvocation.class, () -> {
400 mock2.editABunchMoreStuff();
401
402 new FullVerifications(mock2) {
403 {
404 mock.editABunchMoreStuff();
405 }
406 };
407 });
408 }
409
410
411
412
413
414
415
416 @Test
417 void verifyAllWithUnverifiedReplayOnSameInstance(@Mocked Dependency mock2) {
418 assertThrows(UnexpectedInvocation.class, () -> {
419 mock.editABunchMoreStuff();
420 mock2.editABunchMoreStuff();
421
422 new FullVerifications(mock2) {
423 {
424 mock.editABunchMoreStuff();
425 }
426 };
427 });
428 }
429
430
431
432
433
434
435
436 @Test
437 void verifyStaticInvocationForSpecifiedMockInstance(@Mocked final AnotherDependency mock2) {
438 mock2.doSomething();
439 AnotherDependency.staticMethod();
440 mock2.doSomethingElse(1);
441 mock.editABunchMoreStuff();
442 mock2.doSomethingElse(2);
443
444 new FullVerifications(mock2) {
445 {
446 mock2.doSomething();
447 AnotherDependency.staticMethod();
448 mock2.doSomethingElse(anyInt);
449 mock2.doSomethingElse(anyInt);
450 }
451 };
452 }
453
454
455
456
457
458
459
460 @Test
461 void unverifiedStaticInvocationForSpecifiedMockInstance(@Mocked final AnotherDependency mock2) {
462 assertThrows(UnexpectedInvocation.class, () -> {
463 mock2.doSomething();
464 AnotherDependency.staticMethod();
465
466 new FullVerifications(mock2) {
467 {
468 mock2.doSomething();
469 }
470 };
471 });
472 }
473
474
475
476
477
478
479
480 @Test
481 void unverifiedStaticInvocationForSpecifiedSubclassInstance(@Mocked final SubDependency mock2) {
482 assertThrows(UnexpectedInvocation.class, () -> {
483 mock2.getValue();
484 Dependency.staticMethod("test");
485
486 new FullVerifications(mock2) {
487 {
488 mock2.getValue();
489 }
490 };
491 });
492 }
493
494
495
496
497
498
499
500 @Test
501 void verifyNoInvocationsOccurredOnOneOfTwoMockedDependencies(@Mocked AnotherDependency mock2) {
502 mock2.doSomething();
503
504 new FullVerifications(mock) {
505 };
506 }
507
508
509
510
511
512
513
514 @Test
515 void verifyNoInvocationsOccurredOnMockedDependencyWithOneHavingOccurred(@Mocked AnotherDependency mock2) {
516 mock2.doSomething();
517 mock.editABunchMoreStuff();
518
519 try {
520 new FullVerifications(mock) {
521 };
522 fail();
523 } catch (UnexpectedInvocation e) {
524 assertTrue(e.getMessage().contains("editABunchMoreStuff()"));
525 }
526 }
527
528
529
530
531
532
533
534 @Test
535 void verifyNoInvocationsOnOneOfTwoMockedDependenciesBeyondThoseRecordedAsExpected(
536 @Mocked final AnotherDependency mock2) {
537 new Expectations() {
538 {
539 mock.setSomething(anyInt);
540 mock2.doSomething();
541 times = 1;
542 }
543 };
544
545 mock.prepare();
546 mock.setSomething(1);
547 mock.setSomething(2);
548 mock.save();
549 mock2.doSomething();
550
551 new FullVerifications(mock2) {
552 };
553 }
554 }