View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.fail;
4   
5   import java.util.Date;
6   
7   import mockit.internal.expectations.invocation.MissingInvocation;
8   import mockit.internal.expectations.invocation.UnexpectedInvocation;
9   
10  import org.junit.Test;
11  
12  /**
13   * The Class ExpectationsWithInvocationCountsTest.
14   */
15  public final class ExpectationsWithInvocationCountsTest {
16  
17      /** The code under test. */
18      private final CodeUnderTest codeUnderTest = new CodeUnderTest();
19  
20      /**
21       * The Class CodeUnderTest.
22       */
23      static class CodeUnderTest {
24  
25          /** The dependency. */
26          private final Collaborator dependency = new Collaborator();
27  
28          /**
29           * Do something.
30           */
31          void doSomething() {
32              dependency.provideSomeService();
33          }
34  
35          /**
36           * Do something else.
37           */
38          void doSomethingElse() {
39              dependency.simpleOperation(1, "b", null);
40          }
41      }
42  
43      /**
44       * The Class Collaborator.
45       */
46      static class Collaborator {
47  
48          /**
49           * Instantiates a new collaborator.
50           */
51          Collaborator() {
52          }
53  
54          /**
55           * Instantiates a new collaborator.
56           *
57           * @param value
58           *            the value
59           */
60          Collaborator(@SuppressWarnings("unused") int value) {
61          }
62  
63          /**
64           * Provide some service.
65           */
66          void provideSomeService() {
67          }
68  
69          /**
70           * Simple operation.
71           *
72           * @param a
73           *            the a
74           * @param b
75           *            the b
76           * @param c
77           *            the c
78           */
79          @SuppressWarnings("UnusedDeclaration")
80          final void simpleOperation(int a, String b, Date c) {
81          }
82      }
83  
84      /**
85       * Expect once.
86       *
87       * @param mock
88       *            the mock
89       */
90      @Test
91      public void expectOnce(@Mocked final Collaborator mock) {
92          new Expectations() {
93              {
94                  mock.provideSomeService();
95              }
96          };
97  
98          codeUnderTest.doSomething();
99      }
100 
101     /**
102      * Expect once but replay twice.
103      *
104      * @param mock
105      *            the mock
106      */
107     @Test(expected = UnexpectedInvocation.class)
108     public void expectOnceButReplayTwice(@Mocked final Collaborator mock) {
109         new Expectations() {
110             {
111                 mock.provideSomeService();
112                 times = 1;
113             }
114         };
115 
116         codeUnderTest.doSomething();
117         codeUnderTest.doSomething();
118 
119         fail("Should not get here");
120     }
121 
122     /**
123      * Expect once but replay more times.
124      *
125      * @param mock
126      *            the mock
127      */
128     @Test(expected = UnexpectedInvocation.class)
129     public void expectOnceButReplayMoreTimes(@Mocked final Collaborator mock) {
130         new Expectations() {
131             {
132                 mock.provideSomeService();
133                 times = 1;
134             }
135         };
136 
137         codeUnderTest.doSomething();
138 
139         try {
140             codeUnderTest.doSomething();
141         } finally {
142             codeUnderTest.doSomethingElse();
143         }
144 
145         fail("Should not get here");
146     }
147 
148     /**
149      * Catch unexpected invocation and continue.
150      *
151      * @param mock
152      *            the mock
153      */
154     @Test
155     public void catchUnexpectedInvocationAndContinue(@Mocked final Collaborator mock) {
156         new Expectations() {
157             {
158                 mock.provideSomeService();
159                 maxTimes = 0;
160             }
161         };
162 
163         try {
164             mock.provideSomeService();
165         } catch (UnexpectedInvocation ignore) {
166         }
167 
168         mock.simpleOperation(1, "", null);
169     }
170 
171     /**
172      * Expect twice by using invocation count.
173      *
174      * @param mock
175      *            the mock
176      */
177     @Test
178     public void expectTwiceByUsingInvocationCount(@Mocked final Collaborator mock) {
179         new Expectations() {
180             {
181                 mock.provideSomeService();
182                 times = 2;
183                 mock.simpleOperation(1, "b", null);
184             }
185         };
186 
187         codeUnderTest.doSomething();
188         codeUnderTest.doSomething();
189         codeUnderTest.doSomethingElse();
190     }
191 
192     /**
193      * Expect twice by using invocation count but replay only once.
194      *
195      * @param mock
196      *            the mock
197      */
198     @Test(expected = MissingInvocation.class)
199     public void expectTwiceByUsingInvocationCountButReplayOnlyOnce(@Mocked final Collaborator mock) {
200         new Expectations() {
201             {
202                 mock.provideSomeService();
203                 times = 2;
204                 mock.simpleOperation(1, "b", null);
205             }
206         };
207 
208         codeUnderTest.doSomething();
209         codeUnderTest.doSomethingElse();
210     }
211 
212     /**
213      * Expect exactly twice but replay more times.
214      *
215      * @param mock
216      *            the mock
217      */
218     @Test(expected = UnexpectedInvocation.class)
219     public void expectExactlyTwiceButReplayMoreTimes(@Mocked final Collaborator mock) {
220         new Expectations() {
221             {
222                 mock.provideSomeService();
223                 times = 2;
224             }
225         };
226 
227         codeUnderTest.doSomething();
228         codeUnderTest.doSomething();
229         codeUnderTest.doSomething();
230     }
231 
232     /**
233      * Expect at least once and replay twice.
234      *
235      * @param mock
236      *            the mock
237      */
238     @Test
239     public void expectAtLeastOnceAndReplayTwice(@Mocked final Collaborator mock) {
240         new Expectations() {
241             {
242                 mock.provideSomeService();
243                 minTimes = 1;
244                 mock.simpleOperation(1, "b", null);
245             }
246         };
247 
248         codeUnderTest.doSomething();
249         codeUnderTest.doSomething();
250         codeUnderTest.doSomethingElse();
251     }
252 
253     /**
254      * Expect at least twice but replay once with single expectation.
255      *
256      * @param mock
257      *            the mock
258      */
259     @Test(expected = MissingInvocation.class)
260     public void expectAtLeastTwiceButReplayOnceWithSingleExpectation(@Mocked final Collaborator mock) {
261         new Expectations() {
262             {
263                 mock.provideSomeService();
264                 minTimes = 2;
265             }
266         };
267 
268         codeUnderTest.doSomething();
269     }
270 
271     /**
272      * Expect at least twice but replay once with two consecutive expectations.
273      *
274      * @param mock
275      *            the mock
276      */
277     @Test(expected = MissingInvocation.class)
278     public void expectAtLeastTwiceButReplayOnceWithTwoConsecutiveExpectations(@Mocked final Collaborator mock) {
279         new Expectations() {
280             {
281                 mock.provideSomeService();
282                 minTimes = 2;
283                 mock.simpleOperation(1, "b", null);
284             }
285         };
286 
287         codeUnderTest.doSomething();
288         codeUnderTest.doSomethingElse();
289     }
290 
291     /**
292      * Repeats at least overwriting upper limit.
293      *
294      * @param mock
295      *            the mock
296      */
297     @Test
298     public void repeatsAtLeastOverwritingUpperLimit(@Mocked final Collaborator mock) {
299         new Expectations() {
300             {
301                 mock.provideSomeService();
302                 maxTimes = 2;
303                 minTimes = 1;
304             }
305         };
306 
307         codeUnderTest.doSomething();
308         codeUnderTest.doSomething();
309         codeUnderTest.doSomething();
310     }
311 
312     /**
313      * Expect at most twice and replay once.
314      *
315      * @param mock
316      *            the mock
317      */
318     @Test
319     public void expectAtMostTwiceAndReplayOnce(@Mocked final Collaborator mock) {
320         new Expectations() {
321             {
322                 mock.provideSomeService();
323                 maxTimes = 2;
324                 mock.simpleOperation(1, "b", null);
325             }
326         };
327 
328         codeUnderTest.doSomething();
329         codeUnderTest.doSomethingElse();
330     }
331 
332     /**
333      * Expect at most once but replay twice.
334      *
335      * @param mock
336      *            the mock
337      */
338     @Test(expected = UnexpectedInvocation.class)
339     public void expectAtMostOnceButReplayTwice(@Mocked final Collaborator mock) {
340         new Expectations() {
341             {
342                 mock.provideSomeService();
343                 maxTimes = 1;
344                 mock.simpleOperation(1, "b", null);
345             }
346         };
347 
348         codeUnderTest.doSomething();
349         codeUnderTest.doSomething();
350         codeUnderTest.doSomethingElse();
351     }
352 
353     /**
354      * Repeats at most does not overwrite lower limit.
355      *
356      * @param mock
357      *            the mock
358      */
359     @Test(expected = MissingInvocation.class)
360     public void repeatsAtMostDoesNotOverwriteLowerLimit(@Mocked final Collaborator mock) {
361         new Expectations() {
362             {
363                 mock.provideSomeService();
364                 minTimes = 2;
365                 maxTimes = 3;
366             }
367         };
368 
369         codeUnderTest.doSomething();
370     }
371 
372     /**
373      * Expect same method once or twice then once but replay each expectation only once.
374      *
375      * @param mock
376      *            the mock
377      */
378     @Test
379     public void expectSameMethodOnceOrTwiceThenOnceButReplayEachExpectationOnlyOnce(@Mocked final Collaborator mock) {
380         new Expectations() {
381             {
382                 mock.simpleOperation(1, "", null);
383                 minTimes = 1;
384                 maxTimes = 2;
385                 mock.simpleOperation(2, "", null);
386             }
387         };
388 
389         mock.simpleOperation(1, "", null);
390         mock.simpleOperation(2, "", null);
391     }
392 
393     /**
394      * Expect two or three times.
395      *
396      * @param mock
397      *            the mock
398      */
399     @Test
400     public void expectTwoOrThreeTimes(@Mocked final Collaborator mock) {
401         new Expectations() {
402             {
403                 mock.provideSomeService();
404                 minTimes = 2;
405                 maxTimes = 3;
406                 mock.simpleOperation(1, "b", null);
407             }
408         };
409 
410         codeUnderTest.doSomething();
411         codeUnderTest.doSomething();
412         codeUnderTest.doSomethingElse();
413     }
414 
415     /**
416      * Expect zero or more times and replay twice.
417      *
418      * @param mock
419      *            the mock
420      */
421     @Test
422     public void expectZeroOrMoreTimesAndReplayTwice(@Mocked final Collaborator mock) {
423         new Expectations() {
424             {
425                 mock.provideSomeService();
426                 minTimes = 0;
427                 maxTimes = -1;
428                 mock.simpleOperation(1, "b", null);
429             }
430         };
431 
432         codeUnderTest.doSomething();
433         codeUnderTest.doSomething();
434         codeUnderTest.doSomethingElse();
435     }
436 
437     /**
438      * Expect at least one invocation matching strict expectation but invoke none.
439      *
440      * @param a
441      *            the a
442      */
443     @Test(expected = MissingInvocation.class)
444     public void expectAtLeastOneInvocationMatchingStrictExpectationButInvokeNone(@Mocked final Collaborator a) {
445         new Expectations() {
446             {
447                 a.provideSomeService();
448                 maxTimes = -1;
449             }
450         };
451 
452         // Do nothing at replay time.
453     }
454 
455     /**
456      * Expect one or more invocations followed by another which wont occur max times.
457      *
458      * @param mock
459      *            the mock
460      */
461     @Test(expected = MissingInvocation.class)
462     public void expectOneOrMoreInvocationsFollowedByAnotherWhichWontOccur_maxTimes(@Mocked final Collaborator mock) {
463         new Expectations() {
464             {
465                 mock.provideSomeService();
466                 maxTimes = -1;
467                 mock.simpleOperation(1, null, null);
468             }
469         };
470 
471         codeUnderTest.doSomething();
472     }
473 
474     /**
475      * Expect one or more invocations followed by another which wont occur min times.
476      *
477      * @param mock
478      *            the mock
479      */
480     @Test(expected = MissingInvocation.class)
481     public void expectOneOrMoreInvocationsFollowedByAnotherWhichWontOccur_minTimes(@Mocked final Collaborator mock) {
482         new Expectations() {
483             {
484                 mock.simpleOperation(1, anyString, null);
485                 minTimes = 1;
486                 mock.provideSomeService();
487             }
488         };
489 
490         codeUnderTest.doSomethingElse();
491     }
492 }