1
2
3
4
5
6 package mockit;
7
8 import java.util.Date;
9
10 import mockit.integration.junit5.ExpectedException;
11 import mockit.integration.junit5.JMockitExtension;
12 import mockit.internal.expectations.invocation.MissingInvocation;
13 import mockit.internal.expectations.invocation.UnexpectedInvocation;
14
15 import org.junit.jupiter.api.Assertions;
16 import org.junit.jupiter.api.Test;
17 import org.junit.jupiter.api.extension.ExtendWith;
18
19
20
21
22 @ExtendWith(JMockitExtension.class)
23 class InvocationBlocksWithTimesFieldsTest {
24
25
26 private final CodeUnderTest codeUnderTest = new CodeUnderTest();
27
28
29
30
31 static class CodeUnderTest {
32
33
34 private final Collaborator dependency = new Collaborator();
35
36
37
38
39 void doSomething() {
40 dependency.provideSomeService();
41 }
42
43
44
45
46 void doSomethingElse() {
47 dependency.simpleOperation(1, "b", null);
48 }
49 }
50
51
52
53
54 static class Collaborator {
55
56
57
58
59 Collaborator() {
60 }
61
62
63
64
65
66
67
68 Collaborator(@SuppressWarnings("unused") int value) {
69 }
70
71
72
73
74 void provideSomeService() {
75 }
76
77
78
79
80
81
82
83
84
85
86
87 @SuppressWarnings("UnusedDeclaration")
88 final void simpleOperation(int a, String b, Date c) {
89 }
90 }
91
92
93
94
95
96
97
98
99
100 @Test
101 void expectTwiceByUsingInvocationCount(@Mocked final Collaborator mock) {
102 new Expectations() {
103 {
104 mock.provideSomeService();
105 times = 2;
106 mock.simpleOperation(1, "b", null);
107 }
108 };
109
110 codeUnderTest.doSomething();
111 codeUnderTest.doSomethingElse();
112 codeUnderTest.doSomething();
113 }
114
115
116
117
118
119
120
121 @Test
122 @ExpectedException(MissingInvocation.class)
123 void expectTwiceByUsingInvocationCountButReplayOnlyOnce(@Mocked final Collaborator mock) {
124 new Expectations() {
125 {
126 mock.simpleOperation(1, "b", null);
127 mock.provideSomeService();
128 times = 2;
129 }
130 };
131
132 codeUnderTest.doSomething();
133 codeUnderTest.doSomethingElse();
134 }
135
136
137
138
139
140
141
142 @Test
143 void expectAtLeastOnceAndReplayTwice(@Mocked final Collaborator mock) {
144 new Expectations() {
145 {
146 mock.provideSomeService();
147 mock.simpleOperation(1, "b", null);
148 }
149 };
150
151 codeUnderTest.doSomethingElse();
152 codeUnderTest.doSomething();
153 codeUnderTest.doSomething();
154 }
155
156
157
158
159
160
161
162 @Test
163 void minTimesAndMaxTimesOutOfOrder(@Mocked final Collaborator mock) {
164 new Expectations() {
165 {
166 mock.provideSomeService();
167 maxTimes = 2;
168 minTimes = 1;
169 }
170 };
171
172 codeUnderTest.doSomething();
173 codeUnderTest.doSomething();
174 codeUnderTest.doSomething();
175 }
176
177
178
179
180
181
182
183 @Test
184 void expectAtMostTwiceAndReplayOnce(@Mocked final Collaborator mock) {
185 new Expectations() {
186 {
187 mock.provideSomeService();
188 maxTimes = 2;
189 mock.simpleOperation(1, "b", null);
190 }
191 };
192
193 codeUnderTest.doSomethingElse();
194 codeUnderTest.doSomething();
195 }
196
197
198
199
200
201
202
203 @Test
204 @ExpectedException(MissingInvocation.class)
205 void expectAtMostOnceButReplayTwice(@Mocked final Collaborator mock) {
206 Assertions.assertThrows(UnexpectedInvocation.class, () -> {
207 new Expectations() {
208 {
209 mock.simpleOperation(1, "b", null);
210 mock.provideSomeService();
211 maxTimes = 1;
212 }
213 };
214
215 codeUnderTest.doSomething();
216 codeUnderTest.doSomething();
217 codeUnderTest.doSomethingElse();
218 });
219 }
220
221
222
223
224
225
226
227 @Test
228 void expectAtMostZero(@Mocked final Collaborator mock) {
229 new Expectations() {
230 {
231 mock.provideSomeService();
232 maxTimes = 0;
233 }
234 };
235 }
236
237
238
239
240
241
242
243 @Test
244 @ExpectedException(UnexpectedInvocation.class)
245 void expectAtMostZeroButReplayOnce(@Mocked final Collaborator mock) {
246 new Expectations() {
247 {
248 mock.provideSomeService();
249 maxTimes = 0;
250 }
251 };
252
253 codeUnderTest.doSomething();
254 }
255
256
257
258
259
260
261
262 @Test
263 @ExpectedException(MissingInvocation.class)
264 void maxTimesDoesNotOverwriteMinTimes(@Mocked final Collaborator mock) {
265 new Expectations() {
266 {
267 mock.provideSomeService();
268 minTimes = 2;
269 maxTimes = 3;
270 }
271 };
272
273 codeUnderTest.doSomething();
274 }
275
276
277
278
279
280
281
282 @Test
283 void expectSameMethodOnceOrTwiceThenOnceButReplayEachExpectationOnlyOnce(@Mocked final Collaborator mock) {
284 new Expectations() {
285 {
286 mock.simpleOperation(1, "", null);
287 maxTimes = 2;
288 mock.simpleOperation(2, "", null);
289 }
290 };
291
292 mock.simpleOperation(2, "", null);
293 mock.simpleOperation(1, "", null);
294 }
295
296
297
298
299
300
301
302 @Test
303 void expectTwoOrThreeTimes(@Mocked final Collaborator mock) {
304 new Expectations() {
305 {
306 mock.provideSomeService();
307 minTimes = 2;
308 maxTimes = 3;
309 mock.simpleOperation(1, "b", null);
310 }
311 };
312
313 codeUnderTest.doSomething();
314 codeUnderTest.doSomethingElse();
315 codeUnderTest.doSomething();
316 }
317
318
319
320
321
322
323
324
325
326 @Test
327 void ordered_verifyTwiceByUsingInvocationCount(@Mocked final Collaborator mock) {
328 codeUnderTest.doSomething();
329 codeUnderTest.doSomething();
330 codeUnderTest.doSomethingElse();
331
332 new VerificationsInOrder() {
333 {
334 mock.provideSomeService();
335 times = 2;
336 mock.simpleOperation(1, "b", null);
337 }
338 };
339 }
340
341
342
343
344
345
346
347 @Test
348 @ExpectedException(MissingInvocation.class)
349 void ordered_verifyTwiceByUsingInvocationCountButReplayOnlyOnce(@Mocked final Collaborator mock) {
350 codeUnderTest.doSomethingElse();
351 codeUnderTest.doSomething();
352
353 new VerificationsInOrder() {
354 {
355 mock.simpleOperation(1, "b", null);
356 mock.provideSomeService();
357 times = 2;
358 }
359 };
360 }
361
362
363
364
365
366
367
368 @Test
369 void ordered_verifyAtLeastOnceAndReplayTwice(@Mocked final Collaborator mock) {
370 codeUnderTest.doSomething();
371 codeUnderTest.doSomething();
372 codeUnderTest.doSomethingElse();
373
374 new VerificationsInOrder() {
375 {
376 mock.provideSomeService();
377 mock.simpleOperation(1, "b", null);
378 }
379 };
380 }
381
382
383
384
385
386
387
388 @Test
389 @ExpectedException(UnexpectedInvocation.class)
390 void ordered_minTimesAndMaxTimesOutOfOrder(@Mocked final Collaborator mock) {
391 codeUnderTest.doSomething();
392 codeUnderTest.doSomething();
393 codeUnderTest.doSomething();
394
395 new VerificationsInOrder() {
396 {
397 mock.provideSomeService();
398 maxTimes = 2;
399 minTimes = 1;
400 }
401 };
402 }
403
404
405
406
407
408
409
410 @Test
411 void ordered_verifyAtMostTwiceAndReplayOnce(@Mocked final Collaborator mock) {
412 codeUnderTest.doSomething();
413 codeUnderTest.doSomethingElse();
414
415 new VerificationsInOrder() {
416 {
417 mock.provideSomeService();
418 maxTimes = 2;
419 mock.simpleOperation(1, "b", null);
420 }
421 };
422 }
423
424
425
426
427
428
429
430 @Test
431 @ExpectedException(UnexpectedInvocation.class)
432 void ordered_verifyAtMostOnceButReplayTwice(@Mocked final Collaborator mock) {
433 codeUnderTest.doSomething();
434 codeUnderTest.doSomething();
435 codeUnderTest.doSomethingElse();
436
437 new VerificationsInOrder() {
438 {
439 mock.provideSomeService();
440 maxTimes = 1;
441 mock.simpleOperation(1, "b", null);
442 }
443 };
444 }
445
446
447
448
449
450
451
452 @Test
453 void ordered_verifyAtMostZero(@Mocked final Collaborator mock) {
454 new VerificationsInOrder() {
455 {
456 mock.provideSomeService();
457 maxTimes = 0;
458 }
459 };
460 }
461
462
463
464
465
466
467
468 @Test
469 @ExpectedException(UnexpectedInvocation.class)
470 void ordered_verifyAtMostZeroButReplayOnce(@Mocked final Collaborator mock) {
471 codeUnderTest.doSomething();
472
473 new VerificationsInOrder() {
474 {
475 mock.provideSomeService();
476 maxTimes = 0;
477 }
478 };
479 }
480
481
482
483
484
485
486
487 @Test
488 @ExpectedException(MissingInvocation.class)
489 void ordered_maxTimesDoesNotOverwriteMinTimes(@Mocked final Collaborator mock) {
490 codeUnderTest.doSomething();
491
492 new VerificationsInOrder() {
493 {
494 mock.provideSomeService();
495 minTimes = 2;
496 maxTimes = 3;
497 }
498 };
499 }
500
501
502
503
504
505
506
507 @Test
508 void ordered_verifySameMethodOnceOrTwiceThenOnceButReplayEachExpectationOnlyOnce(@Mocked final Collaborator mock) {
509 mock.simpleOperation(1, "", null);
510 mock.simpleOperation(2, "", null);
511
512 new VerificationsInOrder() {
513 {
514 mock.simpleOperation(1, "", null);
515 maxTimes = 2;
516 mock.simpleOperation(2, "", null);
517 }
518 };
519 }
520
521
522
523
524
525
526
527 @Test
528 void ordered_verifyTwoOrThreeTimes(@Mocked final Collaborator mock) {
529 codeUnderTest.doSomething();
530 codeUnderTest.doSomething();
531 codeUnderTest.doSomethingElse();
532
533 new VerificationsInOrder() {
534 {
535 mock.provideSomeService();
536 minTimes = 2;
537 maxTimes = 3;
538 mock.simpleOperation(1, "b", null);
539 }
540 };
541 }
542
543
544
545
546
547
548
549 @Test
550 @SuppressWarnings("UnusedDeclaration")
551 void ordered_verifyZeroOrMoreTimesAndReplayTwice(@Mocked Collaborator mock) {
552 codeUnderTest.doSomethingElse();
553 codeUnderTest.doSomething();
554 codeUnderTest.doSomething();
555
556 final Collaborator collaborator = new Collaborator();
557
558 new VerificationsInOrder() {
559 {
560 collaborator.simpleOperation(1, "b", null);
561 collaborator.provideSomeService();
562 }
563 };
564 }
565
566
567
568
569
570
571
572 @Test
573 void ordered_verifyZeroOrMoreTimesAndReplayNone(@Mocked final Collaborator mock) {
574 codeUnderTest.doSomethingElse();
575
576 new VerificationsInOrder() {
577 {
578 mock.provideSomeService();
579 minTimes = 0;
580 maxTimes = -1;
581 mock.simpleOperation(1, "b", null);
582 }
583 };
584 }
585
586
587
588
589
590
591
592
593
594 @Test
595 void unordered_verifyTwiceByUsingInvocationCount(@Mocked final Collaborator mock) {
596 codeUnderTest.doSomething();
597 codeUnderTest.doSomethingElse();
598 codeUnderTest.doSomething();
599
600 new Verifications() {
601 {
602 mock.provideSomeService();
603 times = 2;
604 mock.simpleOperation(1, "b", null);
605 }
606 };
607 }
608
609
610
611
612
613
614
615 @Test
616 @ExpectedException(UnexpectedInvocation.class)
617 void unordered_verifyTwiceByUsingInvocationCountButReplayOnlyOnce(@Mocked final Collaborator mock) {
618 Assertions.assertThrows(MissingInvocation.class, () -> {
619 codeUnderTest.doSomethingElse();
620 codeUnderTest.doSomething();
621
622 new FullVerifications() {
623 {
624 mock.provideSomeService();
625 times = 2;
626 mock.simpleOperation(1, "b", null);
627 }
628 };
629 });
630 }
631
632
633
634
635
636
637
638 @Test
639 void unordered_verifyAtLeastOnceAndReplayTwice(@Mocked final Collaborator mock) {
640 codeUnderTest.doSomethingElse();
641 codeUnderTest.doSomething();
642 codeUnderTest.doSomething();
643
644 new Verifications() {
645 {
646 mock.provideSomeService();
647 mock.simpleOperation(1, "b", null);
648 }
649 };
650 }
651
652
653
654
655
656
657
658 @Test
659 @ExpectedException(UnexpectedInvocation.class)
660 void unordered_minTimesAndMaxTimesOutOfOrder(@Mocked final Collaborator mock) {
661 codeUnderTest.doSomething();
662 codeUnderTest.doSomething();
663 codeUnderTest.doSomething();
664
665 new Verifications() {
666 {
667 mock.provideSomeService();
668 maxTimes = 2;
669 minTimes = 1;
670 }
671 };
672 }
673
674
675
676
677
678
679
680 @Test
681 void unordered_verifyAtMostTwiceAndReplayOnce(@Mocked final Collaborator mock) {
682 codeUnderTest.doSomething();
683 codeUnderTest.doSomethingElse();
684
685 new FullVerifications() {
686 {
687 mock.simpleOperation(1, "b", null);
688 mock.provideSomeService();
689 maxTimes = 2;
690 }
691 };
692 }
693
694
695
696
697
698
699
700 @Test
701 @ExpectedException(UnexpectedInvocation.class)
702 void unordered_verifyAtMostOnceButReplayTwice(@Mocked final Collaborator mock) {
703 codeUnderTest.doSomething();
704 codeUnderTest.doSomethingElse();
705 codeUnderTest.doSomething();
706
707 new Verifications() {
708 {
709 mock.provideSomeService();
710 maxTimes = 1;
711 mock.simpleOperation(1, "b", null);
712 }
713 };
714 }
715
716
717
718
719
720
721
722 @Test
723 void unordered_verifyAtMostZero(@Mocked final Collaborator mock) {
724 new Verifications() {
725 {
726 mock.provideSomeService();
727 maxTimes = 0;
728 }
729 };
730 }
731
732
733
734
735
736
737
738 @Test
739 @ExpectedException(UnexpectedInvocation.class)
740 void unordered_verifyAtMostZeroButReplayOnce(@Mocked final Collaborator mock) {
741 codeUnderTest.doSomething();
742
743 new Verifications() {
744 {
745 mock.provideSomeService();
746 maxTimes = 0;
747 }
748 };
749 }
750
751
752
753
754
755
756
757 @Test
758 @ExpectedException(MissingInvocation.class)
759 void unordered_maxTimesDoesNotOverwriteMinTimes(@Mocked final Collaborator mock) {
760 codeUnderTest.doSomething();
761
762 new FullVerifications() {
763 {
764 mock.provideSomeService();
765 minTimes = 2;
766 maxTimes = 3;
767 }
768 };
769 }
770
771
772
773
774
775
776
777 @Test
778 void unordered_verifySameMethodOnceOrTwiceThenOnceButReplayEachExpectationOnlyOnce(
779 @Mocked final Collaborator mock) {
780 mock.simpleOperation(2, "", null);
781 mock.simpleOperation(1, "", null);
782
783 new Verifications() {
784 {
785 mock.simpleOperation(1, "", null);
786 maxTimes = 2;
787 mock.simpleOperation(2, "", null);
788 }
789 };
790 }
791
792
793
794
795
796
797
798 @Test
799 void unordered_verifyTwoOrThreeTimes(@Mocked final Collaborator mock) {
800 codeUnderTest.doSomething();
801 codeUnderTest.doSomethingElse();
802 codeUnderTest.doSomething();
803
804 new FullVerifications() {
805 {
806 mock.simpleOperation(1, "b", null);
807 mock.provideSomeService();
808 minTimes = 2;
809 maxTimes = 3;
810 }
811 };
812 }
813
814
815
816
817
818
819
820 @Test
821 void unordered_verifyOneOrMoreTimesAndReplayTwice(@Mocked Collaborator mock) {
822 codeUnderTest.doSomething();
823 codeUnderTest.doSomethingElse();
824 codeUnderTest.doSomething();
825
826 final Collaborator collaborator = new Collaborator();
827
828 new Verifications() {
829 {
830 collaborator.simpleOperation(1, "b", null);
831 collaborator.provideSomeService();
832 }
833 };
834 }
835 }