View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
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   * The Class InvocationBlocksWithTimesFieldsTest.
21   */
22  @ExtendWith(JMockitExtension.class)
23  class InvocationBlocksWithTimesFieldsTest {
24  
25      /** The code under test. */
26      private final CodeUnderTest codeUnderTest = new CodeUnderTest();
27  
28      /**
29       * The Class CodeUnderTest.
30       */
31      static class CodeUnderTest {
32  
33          /** The dependency. */
34          private final Collaborator dependency = new Collaborator();
35  
36          /**
37           * Do something.
38           */
39          void doSomething() {
40              dependency.provideSomeService();
41          }
42  
43          /**
44           * Do something else.
45           */
46          void doSomethingElse() {
47              dependency.simpleOperation(1, "b", null);
48          }
49      }
50  
51      /**
52       * The Class Collaborator.
53       */
54      static class Collaborator {
55  
56          /**
57           * Instantiates a new collaborator.
58           */
59          Collaborator() {
60          }
61  
62          /**
63           * Instantiates a new collaborator.
64           *
65           * @param value
66           *            the value
67           */
68          Collaborator(@SuppressWarnings("unused") int value) {
69          }
70  
71          /**
72           * Provide some service.
73           */
74          void provideSomeService() {
75          }
76  
77          /**
78           * Simple operation.
79           *
80           * @param a
81           *            the a
82           * @param b
83           *            the b
84           * @param c
85           *            the c
86           */
87          @SuppressWarnings("UnusedDeclaration")
88          final void simpleOperation(int a, String b, Date c) {
89          }
90      }
91  
92      // Tests with recorded expectations ////////////////////////////////////////////////////////////////////////////////
93  
94      /**
95       * Expect twice by using invocation count.
96       *
97       * @param mock
98       *            the mock
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      * Expect twice by using invocation count but replay only once.
117      *
118      * @param mock
119      *            the mock
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      * Expect at least once and replay twice.
138      *
139      * @param mock
140      *            the mock
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      * Min times and max times out of order.
158      *
159      * @param mock
160      *            the mock
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      * Expect at most twice and replay once.
179      *
180      * @param mock
181      *            the mock
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      * Expect at most once but replay twice.
199      *
200      * @param mock
201      *            the mock
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      * Expect at most zero.
223      *
224      * @param mock
225      *            the mock
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      * Expect at most zero but replay once.
239      *
240      * @param mock
241      *            the mock
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      * Max times does not overwrite min times.
258      *
259      * @param mock
260      *            the mock
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      * Expect same method once or twice then once but replay each expectation only once.
278      *
279      * @param mock
280      *            the mock
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      * Expect two or three times.
298      *
299      * @param mock
300      *            the mock
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     // Tests with ordered verifications ////////////////////////////////////////////////////////////////////////////////
319 
320     /**
321      * Ordered verify twice by using invocation count.
322      *
323      * @param mock
324      *            the mock
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      * Ordered verify twice by using invocation count but replay only once.
343      *
344      * @param mock
345      *            the mock
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      * Ordered verify at least once and replay twice.
364      *
365      * @param mock
366      *            the mock
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      * Ordered min times and max times out of order.
384      *
385      * @param mock
386      *            the mock
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      * Ordered verify at most twice and replay once.
406      *
407      * @param mock
408      *            the mock
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      * Ordered verify at most once but replay twice.
426      *
427      * @param mock
428      *            the mock
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      * Ordered verify at most zero.
448      *
449      * @param mock
450      *            the mock
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      * Ordered verify at most zero but replay once.
464      *
465      * @param mock
466      *            the mock
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      * Ordered max times does not overwrite min times.
483      *
484      * @param mock
485      *            the mock
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      * Ordered verify same method once or twice then once but replay each expectation only once.
503      *
504      * @param mock
505      *            the mock
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      * Ordered verify two or three times.
523      *
524      * @param mock
525      *            the mock
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      * Ordered verify zero or more times and replay twice.
545      *
546      * @param mock
547      *            the mock
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      * Ordered verify zero or more times and replay none.
568      *
569      * @param mock
570      *            the mock
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     // Tests with unordered verifications //////////////////////////////////////////////////////////////////////////////
587 
588     /**
589      * Unordered verify twice by using invocation count.
590      *
591      * @param mock
592      *            the mock
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      * Unordered verify twice by using invocation count but replay only once.
611      *
612      * @param mock
613      *            the mock
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      * Unordered verify at least once and replay twice.
634      *
635      * @param mock
636      *            the mock
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      * Unordered min times and max times out of order.
654      *
655      * @param mock
656      *            the mock
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      * Unordered verify at most twice and replay once.
676      *
677      * @param mock
678      *            the mock
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      * Unordered verify at most once but replay twice.
696      *
697      * @param mock
698      *            the mock
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      * Unordered verify at most zero.
718      *
719      * @param mock
720      *            the mock
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      * Unordered verify at most zero but replay once.
734      *
735      * @param mock
736      *            the mock
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      * Unordered max times does not overwrite min times.
753      *
754      * @param mock
755      *            the mock
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      * Unordered verify same method once or twice then once but replay each expectation only once.
773      *
774      * @param mock
775      *            the mock
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      * Unordered verify two or three times.
794      *
795      * @param mock
796      *            the mock
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      * Unordered verify one or more times and replay twice.
816      *
817      * @param mock
818      *            the mock
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 }