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