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