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