View Javadoc
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   * The Class InvocationBlocksWithTimesFieldsTest.
14   */
15  public final class InvocationBlocksWithTimesFieldsTest {
16  
17      /** The thrown. */
18      @Rule
19      public final ExpectedException thrown = ExpectedException.none();
20  
21      /** The code under test. */
22      private final CodeUnderTest codeUnderTest = new CodeUnderTest();
23  
24      /**
25       * The Class CodeUnderTest.
26       */
27      static class CodeUnderTest {
28  
29          /** The dependency. */
30          private final Collaborator dependency = new Collaborator();
31  
32          /**
33           * Do something.
34           */
35          void doSomething() {
36              dependency.provideSomeService();
37          }
38  
39          /**
40           * Do something else.
41           */
42          void doSomethingElse() {
43              dependency.simpleOperation(1, "b", null);
44          }
45      }
46  
47      /**
48       * The Class Collaborator.
49       */
50      static class Collaborator {
51  
52          /**
53           * Instantiates a new collaborator.
54           */
55          Collaborator() {
56          }
57  
58          /**
59           * Instantiates a new collaborator.
60           *
61           * @param value
62           *            the value
63           */
64          Collaborator(@SuppressWarnings("unused") int value) {
65          }
66  
67          /**
68           * Provide some service.
69           */
70          void provideSomeService() {
71          }
72  
73          /**
74           * Simple operation.
75           *
76           * @param a
77           *            the a
78           * @param b
79           *            the b
80           * @param c
81           *            the c
82           */
83          @SuppressWarnings("UnusedDeclaration")
84          final void simpleOperation(int a, String b, Date c) {
85          }
86      }
87  
88      // Tests with recorded expectations ////////////////////////////////////////////////////////////////////////////////
89  
90      /**
91       * Expect twice by using invocation count.
92       *
93       * @param mock
94       *            the mock
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      * Expect twice by using invocation count but replay only once.
113      *
114      * @param mock
115      *            the mock
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      * Expect at least once and replay twice.
135      *
136      * @param mock
137      *            the mock
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      * Min times and max times out of order.
155      *
156      * @param mock
157      *            the mock
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      * Expect at most twice and replay once.
176      *
177      * @param mock
178      *            the mock
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      * Expect at most once but replay twice.
196      *
197      * @param mock
198      *            the mock
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      * Expect at most zero.
219      *
220      * @param mock
221      *            the mock
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      * Expect at most zero but replay once.
235      *
236      * @param mock
237      *            the mock
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      * Max times does not overwrite min times.
255      *
256      * @param mock
257      *            the mock
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      * Expect same method once or twice then once but replay each expectation only once.
276      *
277      * @param mock
278      *            the mock
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      * Expect two or three times.
296      *
297      * @param mock
298      *            the mock
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     // Tests with ordered verifications ////////////////////////////////////////////////////////////////////////////////
317 
318     /**
319      * Ordered verify twice by using invocation count.
320      *
321      * @param mock
322      *            the mock
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      * Ordered verify twice by using invocation count but replay only once.
341      *
342      * @param mock
343      *            the mock
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      * Ordered verify at least once and replay twice.
363      *
364      * @param mock
365      *            the mock
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      * Ordered min times and max times out of order.
383      *
384      * @param mock
385      *            the mock
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      * Ordered verify at most twice and replay once.
406      *
407      * @param mock
408      *            the mock
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      * Ordered verify at most once but replay twice.
426      *
427      * @param mock
428      *            the mock
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      * Ordered verify at most zero.
449      *
450      * @param mock
451      *            the mock
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      * Ordered verify at most zero but replay once.
465      *
466      * @param mock
467      *            the mock
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      * Ordered max times does not overwrite min times.
485      *
486      * @param mock
487      *            the mock
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      * Ordered verify same method once or twice then once but replay each expectation only once.
506      *
507      * @param mock
508      *            the mock
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      * Ordered verify two or three times.
527      *
528      * @param mock
529      *            the mock
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      * Ordered verify zero or more times and replay twice.
549      *
550      * @param mock
551      *            the mock
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      * Ordered verify zero or more times and replay none.
572      *
573      * @param mock
574      *            the mock
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     // Tests with unordered verifications //////////////////////////////////////////////////////////////////////////////
591 
592     /**
593      * Unordered verify twice by using invocation count.
594      *
595      * @param mock
596      *            the mock
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      * Unordered verify twice by using invocation count but replay only once.
615      *
616      * @param mock
617      *            the mock
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      * Unordered verify at least once and replay twice.
637      *
638      * @param mock
639      *            the mock
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      * Unordered min times and max times out of order.
657      *
658      * @param mock
659      *            the mock
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      * Unordered verify at most twice and replay once.
680      *
681      * @param mock
682      *            the mock
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      * Unordered verify at most once but replay twice.
700      *
701      * @param mock
702      *            the mock
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      * Unordered verify at most zero.
723      *
724      * @param mock
725      *            the mock
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      * Unordered verify at most zero but replay once.
739      *
740      * @param mock
741      *            the mock
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      * Unordered max times does not overwrite min times.
759      *
760      * @param mock
761      *            the mock
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      * Unordered verify same method once or twice then once but replay each expectation only once.
780      *
781      * @param mock
782      *            the mock
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      * Unordered verify two or three times.
801      *
802      * @param mock
803      *            the mock
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      * Unordered verify one or more times and replay twice.
823      *
824      * @param mock
825      *            the mock
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 }