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 static org.junit.jupiter.api.Assertions.assertThrows;
9   import static org.junit.jupiter.api.Assertions.assertTrue;
10  import static org.junit.jupiter.api.Assertions.fail;
11  
12  import java.util.concurrent.Callable;
13  
14  import mockit.integration.junit5.JMockitExtension;
15  import mockit.internal.expectations.invocation.MissingInvocation;
16  import mockit.internal.expectations.invocation.UnexpectedInvocation;
17  
18  import org.junit.jupiter.api.Test;
19  import org.junit.jupiter.api.extension.ExtendWith;
20  
21  /**
22   * The Class RestrictedFullVerificationsTest.
23   */
24  @ExtendWith(JMockitExtension.class)
25  class RestrictedFullVerificationsTest {
26  
27      /**
28       * The Class Dependency.
29       */
30      @SuppressWarnings("UnusedReturnValue")
31      static class Dependency {
32  
33          /**
34           * Sets the something.
35           *
36           * @param value
37           *            the new something
38           */
39          public void setSomething(@SuppressWarnings("unused") int value) {
40          }
41  
42          /**
43           * Edits the A bunch more stuff.
44           *
45           * @return the int
46           */
47          public int editABunchMoreStuff() {
48              return 0;
49          }
50  
51          /**
52           * Prepare.
53           *
54           * @return true, if successful
55           */
56          public boolean prepare() {
57              return false;
58          }
59  
60          /**
61           * Save.
62           */
63          public void save() {
64          }
65  
66          /**
67           * Static method.
68           *
69           * @param s
70           *            the s
71           */
72          static void staticMethod(@SuppressWarnings("unused") String s) {
73          }
74      }
75  
76      /**
77       * The Class SubDependency.
78       */
79      static final class SubDependency extends Dependency {
80          /**
81           * Gets the value.
82           *
83           * @return the value
84           */
85          int getValue() {
86              return 5;
87          }
88      }
89  
90      /**
91       * The Class AnotherDependency.
92       */
93      static final class AnotherDependency {
94  
95          /**
96           * Do something.
97           */
98          void doSomething() {
99          }
100 
101         /**
102          * Do something else.
103          *
104          * @param i
105          *            the i
106          *
107          * @return the string
108          */
109         String doSomethingElse(int i) {
110             return String.valueOf(i);
111         }
112 
113         /**
114          * Static method.
115          *
116          * @return true, if successful
117          */
118         static boolean staticMethod() {
119             return true;
120         }
121     }
122 
123     /** The mock. */
124     @Mocked
125     Dependency mock;
126 
127     /**
128      * Exercise code under test.
129      */
130     void exerciseCodeUnderTest() {
131         mock.prepare();
132         mock.setSomething(123);
133         mock.editABunchMoreStuff();
134         mock.save();
135     }
136 
137     /**
138      * Verify all invocations to only one of two mocked types.
139      *
140      * @param mock2
141      *            the mock 2
142      */
143     @Test
144     void verifyAllInvocationsToOnlyOneOfTwoMockedTypes(@Mocked AnotherDependency mock2) {
145         exerciseCodeUnderTest();
146         mock2.doSomething();
147 
148         new FullVerifications(mock) {
149             {
150                 mock.prepare();
151                 mock.setSomething(anyInt);
152                 minTimes = 1;
153                 maxTimes = 2;
154                 mock.editABunchMoreStuff();
155                 mock.save();
156                 times = 1;
157             }
158         };
159 
160         new FullVerifications(mock.getClass()) {
161             {
162                 mock.prepare();
163                 mock.setSomething(anyInt);
164                 minTimes = 1;
165                 maxTimes = 2;
166                 mock.editABunchMoreStuff();
167                 mock.save();
168                 times = 1;
169             }
170         };
171     }
172 
173     /**
174      * Verify all invocations with some missing.
175      *
176      * @param mock2
177      *            the mock 2
178      */
179     @Test
180     void verifyAllInvocationsWithSomeMissing(@Mocked final AnotherDependency mock2) {
181         assertThrows(UnexpectedInvocation.class, () -> {
182             exerciseCodeUnderTest();
183             mock2.doSomething();
184 
185             new FullVerifications(mock, mock2) {
186                 {
187                     mock.prepare();
188                     mock.setSomething(anyInt);
189                     mock.save();
190                     mock2.doSomething();
191                 }
192             };
193         });
194     }
195 
196     /**
197      * Verify only invocations to generic type.
198      *
199      * @param mock2
200      *            the mock 2
201      *
202      * @throws Exception
203      *             the exception
204      */
205     @Test
206     void verifyOnlyInvocationsToGenericType(@Mocked final Callable<Dependency> mock2) throws Exception {
207         exerciseCodeUnderTest();
208         mock2.call();
209 
210         new FullVerifications(mock2) {
211             {
212                 mock2.call();
213             }
214         };
215     }
216 
217     /**
218      * Verify all invocations to inherited methods.
219      *
220      * @param mock2
221      *            the mock 2
222      */
223     @Test
224     void verifyAllInvocationsToInheritedMethods(@Mocked SubDependency mock2) {
225         mock.prepare();
226         mock2.getValue();
227 
228         new FullVerifications(mock) {
229             {
230                 mock.prepare();
231             }
232         };
233         new FullVerifications(Dependency.class) {
234             {
235                 mock.prepare();
236             }
237         };
238     }
239 
240     /**
241      * Verify all invocations to inherited methods when not verified.
242      *
243      * @param mock2
244      *            the mock 2
245      */
246     @Test
247     void verifyAllInvocationsToInheritedMethods_whenNotVerified(@Mocked final SubDependency mock2) {
248         assertThrows(UnexpectedInvocation.class, () -> {
249             mock2.prepare();
250             mock2.getValue();
251 
252             new FullVerifications(mock2) {
253                 {
254                     mock2.getValue();
255                 }
256             };
257         });
258     }
259 
260     /**
261      * Verify all invocations to subclass methods.
262      *
263      * @param mock2
264      *            the mock 2
265      */
266     @Test
267     void verifyAllInvocationsToSubclassMethods(@Mocked final SubDependency mock2) {
268         mock.prepare();
269         mock2.getValue();
270 
271         new FullVerifications(mock2.getClass()) {
272             {
273                 mock2.getValue();
274             }
275         };
276     }
277 
278     /**
279      * Verify all invocations to subclass methods when not verified.
280      *
281      * @param mock2
282      *            the mock 2
283      */
284     @Test
285     void verifyAllInvocationsToSubclassMethods_whenNotVerified(@Mocked SubDependency mock2) {
286         assertThrows(UnexpectedInvocation.class, () -> {
287             mock.prepare();
288             mock2.getValue();
289 
290             new FullVerifications(mock2.getClass()) {
291                 {
292                     mock.prepare();
293                 }
294             };
295         });
296     }
297 
298     /**
299      * Verify all invocations to methods of base class and of subclass.
300      *
301      * @param mock2
302      *            the mock 2
303      */
304     @Test
305     void verifyAllInvocationsToMethodsOfBaseClassAndOfSubclass(@Mocked final SubDependency mock2) {
306         mock2.prepare();
307         mock2.getValue();
308 
309         new FullVerifications(mock2) {
310             {
311                 mock2.prepare();
312                 mock2.getValue();
313             }
314         };
315     }
316 
317     /**
318      * Verify all invocations to methods of base class and of subclass when inherited method not verified.
319      *
320      * @param mock2
321      *            the mock 2
322      */
323     @Test
324     void verifyAllInvocationsToMethodsOfBaseClassAndOfSubclass_whenInheritedMethodNotVerified(
325             @Mocked final SubDependency mock2) {
326         assertThrows(UnexpectedInvocation.class, () -> {
327             mock2.prepare();
328             mock2.getValue();
329 
330             new FullVerifications(mock2) {
331                 {
332                     mock2.getValue();
333                 }
334             };
335         });
336     }
337 
338     /**
339      * Verify all invocations to methods of base class and of subclass when subclass method not verified.
340      *
341      * @param mock2
342      *            the mock 2
343      */
344     @Test
345     void verifyAllInvocationsToMethodsOfBaseClassAndOfSubclass_whenSubclassMethodNotVerified(
346             @Mocked SubDependency mock2) {
347         assertThrows(UnexpectedInvocation.class, () -> {
348             mock.prepare();
349             mock2.getValue();
350 
351             new FullVerifications(mock2) {
352                 {
353                     mock.prepare();
354                 }
355             };
356         });
357     }
358 
359     /**
360      * Verify all invocations with replay on different instance.
361      */
362     @Test
363     void verifyAllInvocationsWithReplayOnDifferentInstance() {
364         new Dependency().save();
365 
366         new FullVerifications(mock) {
367             {
368                 new Dependency();
369                 mock.save();
370             }
371         };
372     }
373 
374     /**
375      * Verify all invocations with replay on same instance.
376      *
377      * @param mock2
378      *            the mock 2
379      */
380     @Test
381     void verifyAllInvocationsWithReplayOnSameInstance(@Mocked final Dependency mock2) {
382         mock2.editABunchMoreStuff();
383 
384         new FullVerifications(mock2) {
385             {
386                 mock2.editABunchMoreStuff();
387             }
388         };
389     }
390 
391     /**
392      * Verify all with replay on different instance when should be same.
393      *
394      * @param mock2
395      *            the mock 2
396      */
397     @Test
398     void verifyAllWithReplayOnDifferentInstanceWhenShouldBeSame(@Mocked Dependency mock2) {
399         assertThrows(MissingInvocation.class, () -> {
400             mock2.editABunchMoreStuff();
401 
402             new FullVerifications(mock2) {
403                 {
404                     mock.editABunchMoreStuff();
405                 }
406             };
407         });
408     }
409 
410     /**
411      * Verify all with unverified replay on same instance.
412      *
413      * @param mock2
414      *            the mock 2
415      */
416     @Test
417     void verifyAllWithUnverifiedReplayOnSameInstance(@Mocked Dependency mock2) {
418         assertThrows(UnexpectedInvocation.class, () -> {
419             mock.editABunchMoreStuff();
420             mock2.editABunchMoreStuff();
421 
422             new FullVerifications(mock2) {
423                 {
424                     mock.editABunchMoreStuff();
425                 }
426             };
427         });
428     }
429 
430     /**
431      * Verify static invocation for specified mock instance.
432      *
433      * @param mock2
434      *            the mock 2
435      */
436     @Test
437     void verifyStaticInvocationForSpecifiedMockInstance(@Mocked final AnotherDependency mock2) {
438         mock2.doSomething();
439         AnotherDependency.staticMethod();
440         mock2.doSomethingElse(1);
441         mock.editABunchMoreStuff();
442         mock2.doSomethingElse(2);
443 
444         new FullVerifications(mock2) {
445             {
446                 mock2.doSomething();
447                 AnotherDependency.staticMethod();
448                 mock2.doSomethingElse(anyInt);
449                 mock2.doSomethingElse(anyInt);
450             }
451         };
452     }
453 
454     /**
455      * Unverified static invocation for specified mock instance.
456      *
457      * @param mock2
458      *            the mock 2
459      */
460     @Test
461     void unverifiedStaticInvocationForSpecifiedMockInstance(@Mocked final AnotherDependency mock2) {
462         assertThrows(UnexpectedInvocation.class, () -> {
463             mock2.doSomething();
464             AnotherDependency.staticMethod();
465 
466             new FullVerifications(mock2) {
467                 {
468                     mock2.doSomething();
469                 }
470             };
471         });
472     }
473 
474     /**
475      * Unverified static invocation for specified subclass instance.
476      *
477      * @param mock2
478      *            the mock 2
479      */
480     @Test
481     void unverifiedStaticInvocationForSpecifiedSubclassInstance(@Mocked final SubDependency mock2) {
482         assertThrows(UnexpectedInvocation.class, () -> {
483             mock2.getValue();
484             Dependency.staticMethod("test");
485 
486             new FullVerifications(mock2) {
487                 {
488                     mock2.getValue();
489                 }
490             };
491         });
492     }
493 
494     /**
495      * Verify no invocations occurred on one of two mocked dependencies.
496      *
497      * @param mock2
498      *            the mock 2
499      */
500     @Test
501     void verifyNoInvocationsOccurredOnOneOfTwoMockedDependencies(@Mocked AnotherDependency mock2) {
502         mock2.doSomething();
503 
504         new FullVerifications(mock) {
505         };
506     }
507 
508     /**
509      * Verify no invocations occurred on mocked dependency with one having occurred.
510      *
511      * @param mock2
512      *            the mock 2
513      */
514     @Test
515     void verifyNoInvocationsOccurredOnMockedDependencyWithOneHavingOccurred(@Mocked AnotherDependency mock2) {
516         mock2.doSomething();
517         mock.editABunchMoreStuff();
518 
519         try {
520             new FullVerifications(mock) {
521             };
522             fail();
523         } catch (UnexpectedInvocation e) {
524             assertTrue(e.getMessage().contains("editABunchMoreStuff()"));
525         }
526     }
527 
528     /**
529      * Verify no invocations on one of two mocked dependencies beyond those recorded as expected.
530      *
531      * @param mock2
532      *            the mock 2
533      */
534     @Test
535     void verifyNoInvocationsOnOneOfTwoMockedDependenciesBeyondThoseRecordedAsExpected(
536             @Mocked final AnotherDependency mock2) {
537         new Expectations() {
538             {
539                 mock.setSomething(anyInt);
540                 mock2.doSomething();
541                 times = 1;
542             }
543         };
544 
545         mock.prepare();
546         mock.setSomething(1);
547         mock.setSomething(2);
548         mock.save();
549         mock2.doSomething();
550 
551         new FullVerifications(mock2) {
552         };
553     }
554 }