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