View Javadoc
1   package mockit;
2   
3   import mockit.internal.expectations.invocation.MissingInvocation;
4   import mockit.internal.expectations.invocation.UnexpectedInvocation;
5   
6   import org.junit.Rule;
7   import org.junit.Test;
8   import org.junit.rules.ExpectedException;
9   
10  /**
11   * The Class FullVerificationsTest.
12   */
13  public final class FullVerificationsTest {
14  
15      /** The thrown. */
16      @Rule
17      public final ExpectedException thrown = ExpectedException.none();
18  
19      /**
20       * The Class Dependency.
21       */
22      public static class Dependency {
23  
24          /**
25           * Sets the something.
26           *
27           * @param value
28           *            the new something
29           */
30          public void setSomething(@SuppressWarnings("unused") int value) {
31          }
32  
33          /**
34           * Sets the something else.
35           *
36           * @param value
37           *            the new something else
38           */
39          public void setSomethingElse(@SuppressWarnings("unused") char value) {
40          }
41  
42          /**
43           * Edits the A bunch more stuff.
44           *
45           * @return true, if successful
46           */
47          public boolean editABunchMoreStuff() {
48              return false;
49          }
50  
51          /**
52           * Notify before save.
53           */
54          public void notifyBeforeSave() {
55          }
56  
57          /**
58           * Prepare.
59           */
60          public void prepare() {
61          }
62  
63          /**
64           * Save.
65           */
66          public void save() {
67          }
68      }
69  
70      /** The mock. */
71      @Mocked
72      Dependency mock;
73  
74      /**
75       * Exercise code under test.
76       */
77      void exerciseCodeUnderTest() {
78          mock.prepare();
79          mock.setSomething(123);
80          mock.setSomethingElse('a');
81          mock.setSomething(45);
82          mock.editABunchMoreStuff();
83          mock.notifyBeforeSave();
84          mock.save();
85      }
86  
87      /**
88       * Verify all invocations.
89       */
90      @Test
91      public void verifyAllInvocations() {
92          exerciseCodeUnderTest();
93  
94          new FullVerifications() {
95              {
96                  mock.prepare();
97                  minTimes = 1;
98                  mock.editABunchMoreStuff();
99                  mock.notifyBeforeSave();
100                 maxTimes = 1;
101                 mock.setSomething(anyInt);
102                 minTimes = 0;
103                 maxTimes = 2;
104                 mock.setSomethingElse(anyChar);
105                 mock.save();
106                 times = 1;
107             }
108         };
109     }
110 
111     /**
112      * Verify all invocations with some of them recorded.
113      */
114     @Test
115     public void verifyAllInvocationsWithSomeOfThemRecorded() {
116         new Expectations() {
117             {
118                 mock.editABunchMoreStuff();
119                 result = true;
120                 mock.setSomething(45);
121             }
122         };
123 
124         exerciseCodeUnderTest();
125 
126         new FullVerifications() {
127             {
128                 mock.prepare();
129                 mock.setSomething(anyInt);
130                 mock.setSomethingElse(anyChar);
131                 mock.notifyBeforeSave();
132                 mock.save();
133             }
134         };
135     }
136 
137     /**
138      * Verify all invocations with those recorded as expected to occur verified implicitly.
139      */
140     @Test
141     public void verifyAllInvocationsWithThoseRecordedAsExpectedToOccurVerifiedImplicitly() {
142         new Expectations() {
143             {
144                 mock.setSomething(45);
145                 times = 1;
146                 mock.editABunchMoreStuff();
147                 result = true;
148                 minTimes = 1;
149             }
150         };
151 
152         exerciseCodeUnderTest();
153 
154         new FullVerifications() {
155             {
156                 mock.prepare();
157                 mock.setSomething(123);
158                 mock.setSomethingElse(anyChar);
159                 mock.notifyBeforeSave();
160                 mock.save();
161             }
162         };
163     }
164 
165     /**
166      * Verify all invocations except those already verified in A previous verification block.
167      */
168     @Test
169     public void verifyAllInvocationsExceptThoseAlreadyVerifiedInAPreviousVerificationBlock() {
170         exerciseCodeUnderTest();
171 
172         new Verifications() {
173             {
174                 mock.setSomething(45);
175                 mock.editABunchMoreStuff();
176             }
177         };
178 
179         new FullVerifications() {
180             {
181                 mock.prepare();
182                 mock.setSomething(123);
183                 mock.setSomethingElse(anyChar);
184                 mock.notifyBeforeSave();
185                 mock.save();
186             }
187         };
188     }
189 
190     /**
191      * Verify all invocations with one missing.
192      */
193     @Test
194     public void verifyAllInvocationsWithOneMissing() {
195         thrown.expect(UnexpectedInvocation.class);
196         thrown.expectMessage("editABunchMoreStuff()");
197 
198         exerciseCodeUnderTest();
199 
200         new FullVerifications() {
201             {
202                 mock.prepare();
203                 mock.notifyBeforeSave();
204                 mock.setSomething(anyInt);
205                 mock.setSomethingElse(anyChar);
206                 mock.save();
207             }
208         };
209     }
210 
211     /**
212      * Verify unrecorded invocation that was expected to not happen.
213      */
214     @Test
215     public void verifyUnrecordedInvocationThatWasExpectedToNotHappen() {
216         mock.prepare();
217         mock.setSomething(123);
218         mock.setSomething(45);
219 
220         new FullVerifications() {
221             {
222                 mock.prepare();
223                 mock.setSomething(anyInt);
224                 times = 2;
225                 mock.notifyBeforeSave();
226                 times = 0;
227             }
228         };
229     }
230 
231     /**
232      * Verify unrecorded invocation that should not happen but does.
233      */
234     @Test
235     public void verifyUnrecordedInvocationThatShouldNotHappenButDoes() {
236         thrown.expect(UnexpectedInvocation.class);
237         thrown.expectMessage("1 unexpected invocation");
238 
239         mock.setSomething(1);
240         mock.notifyBeforeSave();
241 
242         new FullVerifications() {
243             {
244                 mock.setSomething(1);
245                 mock.notifyBeforeSave();
246                 times = 0;
247             }
248         };
249     }
250 
251     /**
252      * Verify invocation that is allowed to happen any number of times and happens once.
253      */
254     @Test
255     public void verifyInvocationThatIsAllowedToHappenAnyNumberOfTimesAndHappensOnce() {
256         mock.prepare();
257         mock.setSomething(123);
258         mock.save();
259 
260         new FullVerifications() {
261             {
262                 mock.prepare();
263                 mock.setSomething(anyInt);
264                 mock.save();
265                 minTimes = 0;
266             }
267         };
268     }
269 
270     /**
271      * Verify recorded invocation that is allowed to happen any no of times and does not happen.
272      */
273     @Test
274     public void verifyRecordedInvocationThatIsAllowedToHappenAnyNoOfTimesAndDoesNotHappen() {
275         mock.prepare();
276         mock.setSomething(123);
277 
278         new FullVerifications() {
279             {
280                 mock.prepare();
281                 mock.setSomething(anyInt);
282                 mock.save();
283                 minTimes = 0;
284             }
285         };
286     }
287 
288     /**
289      * Verify unrecorded invocation that is allowed to happen any no of times and does not happen.
290      */
291     @Test
292     public void verifyUnrecordedInvocationThatIsAllowedToHappenAnyNoOfTimesAndDoesNotHappen() {
293         mock.prepare();
294         mock.setSomething(123);
295 
296         new FullVerifications() {
297             {
298                 mock.prepare();
299                 mock.setSomething(anyInt);
300                 mock.save();
301                 minTimes = 0;
302             }
303         };
304     }
305 
306     /**
307      * Verify unrecorded invocation that should happen but does not.
308      */
309     @Test
310     public void verifyUnrecordedInvocationThatShouldHappenButDoesNot() {
311         mock.setSomething(1);
312 
313         thrown.expect(MissingInvocation.class);
314 
315         new FullVerifications() {
316             {
317                 mock.notifyBeforeSave();
318             }
319         };
320     }
321 
322     /**
323      * Verify recorded invocation that should happen but does not.
324      */
325     @Test
326     public void verifyRecordedInvocationThatShouldHappenButDoesNot() {
327         new Expectations() {
328             {
329                 mock.notifyBeforeSave();
330             }
331         };
332         thrown.expect(MissingInvocation.class);
333 
334         mock.setSomething(1);
335 
336         new FullVerifications() {
337             {
338                 mock.notifyBeforeSave();
339             }
340         };
341     }
342 
343     /**
344      * Verify all invocations with extra verification.
345      */
346     @Test
347     public void verifyAllInvocationsWithExtraVerification() {
348         mock.prepare();
349         mock.setSomething(123);
350 
351         thrown.expect(MissingInvocation.class);
352         thrown.expectMessage("notifyBeforeSave()");
353 
354         new FullVerifications() {
355             {
356                 mock.prepare();
357                 mock.setSomething(123);
358                 mock.notifyBeforeSave();
359             }
360         };
361     }
362 
363     /**
364      * Verify all invocations with invocation count one less than actual.
365      */
366     @Test
367     public void verifyAllInvocationsWithInvocationCountOneLessThanActual() {
368         mock.setSomething(123);
369         mock.setSomething(45);
370 
371         thrown.expect(UnexpectedInvocation.class);
372         thrown.expectMessage("45");
373 
374         new FullVerifications() {
375             {
376                 mock.setSomething(anyInt);
377                 times = 1;
378             }
379         };
380     }
381 
382     /**
383      * Verify all invocations with invocation count two less than actual.
384      */
385     @Test
386     public void verifyAllInvocationsWithInvocationCountTwoLessThanActual() {
387         mock.setSomething(123);
388         mock.setSomething(45);
389         mock.setSomething(1);
390 
391         thrown.expect(UnexpectedInvocation.class);
392         thrown.expectMessage("2 unexpected invocations");
393         thrown.expectMessage("1");
394 
395         new FullVerifications() {
396             {
397                 mock.setSomething(anyInt);
398                 times = 1;
399             }
400         };
401     }
402 
403     /**
404      * Verify all invocations with invocation count more than actual.
405      */
406     @Test
407     public void verifyAllInvocationsWithInvocationCountMoreThanActual() {
408         mock.setSomethingElse('f');
409 
410         thrown.expect(MissingInvocation.class);
411         thrown.expectMessage("Missing 2 invocations");
412         thrown.expectMessage("any char");
413 
414         new FullVerifications() {
415             {
416                 mock.setSomethingElse(anyChar);
417                 times = 3;
418             }
419         };
420     }
421 
422     /**
423      * Verify no invocations occurred on mocked dependency with one having occurred.
424      */
425     @Test
426     public void verifyNoInvocationsOccurredOnMockedDependencyWithOneHavingOccurred() {
427         mock.editABunchMoreStuff();
428 
429         thrown.expect(UnexpectedInvocation.class);
430 
431         new FullVerifications() {
432         };
433     }
434 
435     /**
436      * Verify no invocations on mocked dependency beyond those recorded as expected.
437      */
438     @Test
439     public void verifyNoInvocationsOnMockedDependencyBeyondThoseRecordedAsExpected() {
440         new Expectations() {
441             {
442                 mock.prepare();
443                 times = 1;
444             }
445         };
446 
447         new Expectations() {
448             {
449                 mock.setSomething(anyInt);
450                 minTimes = 1;
451                 mock.save();
452                 times = 1;
453             }
454         };
455 
456         mock.prepare();
457         mock.setSomething(1);
458         mock.setSomething(2);
459         mock.save();
460 
461         new FullVerifications() {
462         };
463     }
464 
465     /**
466      * Verify no invocations on mocked dependency beyond those recorded as expected with one having occurred.
467      */
468     @Test
469     public void verifyNoInvocationsOnMockedDependencyBeyondThoseRecordedAsExpectedWithOneHavingOccurred() {
470         thrown.expect(UnexpectedInvocation.class);
471         thrown.expectMessage("editABunchMoreStuff()");
472 
473         new Expectations() {
474             {
475                 mock.prepare();
476                 times = 1;
477                 mock.save();
478                 minTimes = 1;
479             }
480         };
481 
482         mock.prepare();
483         mock.editABunchMoreStuff();
484         mock.save();
485 
486         new FullVerifications() {
487         };
488     }
489 
490     /**
491      * Verify no unverified invocations when first invocation of method is but second one is not.
492      */
493     @Test
494     public void verifyNoUnverifiedInvocationsWhenFirstInvocationOfMethodIsButSecondOneIsNot() {
495         mock.prepare();
496         mock.save();
497         mock.prepare();
498         mock.save(); // doesn't get verified
499 
500         new VerificationsInOrder() {
501             {
502                 mock.prepare();
503                 times = 1;
504                 mock.save();
505                 times = 1;
506                 mock.prepare();
507                 times = 1;
508             }
509         };
510 
511         thrown.expect(UnexpectedInvocation.class);
512         thrown.expectMessage("save()");
513 
514         new FullVerifications() {
515         };
516     }
517 
518     /**
519      * Verify no unverified invocations when second invocation of method is but first one is not.
520      */
521     @Test
522     public void verifyNoUnverifiedInvocationsWhenSecondInvocationOfMethodIsButFirstOneIsNot() {
523         mock.save(); // doesn't get verified
524         mock.prepare();
525         mock.save();
526 
527         new VerificationsInOrder() {
528             {
529                 mock.prepare();
530                 mock.save();
531             }
532         };
533 
534         thrown.expect(UnexpectedInvocation.class);
535         thrown.expectMessage("save()");
536 
537         new FullVerifications() {
538         };
539     }
540 }