1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.verification;
6 
7 import org.junit.Rule;
8 import org.junit.Test;
9 import org.junit.rules.ExpectedException;
10 import org.mockito.InOrder;
11 import org.mockito.Mock;
12 import org.mockito.exceptions.base.MockitoException;
13 import org.mockito.exceptions.verification.NoInteractionsWanted;
14 import org.mockito.exceptions.verification.VerificationInOrderFailure;
15 import org.mockitousage.IMethods;
16 import org.mockitoutil.TestBase;
17 
18 import static org.mockito.Mockito.*;
19 
20 public class VerificationInOrderWithCallsTest extends TestBase {
21 
22     @Mock private IMethods mockOne;
23     @Mock private IMethods mockTwo;
24     @Rule
25     public ExpectedException exceptionRule = ExpectedException.none();
26 
27     @Test
shouldFailWhenMethodNotCalled()28     public void shouldFailWhenMethodNotCalled(){
29         // Given
30         mockOne.oneArg( 1 );
31         InOrder verifier = inOrder( mockOne );
32         verifier.verify( mockOne, calls(1)).oneArg( 1 );
33 
34         exceptionRule.expect( VerificationInOrderFailure.class );
35         exceptionRule.expectMessage( "Verification in order failure" );
36         exceptionRule.expectMessage( "Wanted but not invoked" );
37         exceptionRule.expectMessage( "mockOne.oneArg(2)" );
38 
39         // When
40         verifier.verify( mockOne, calls(1)).oneArg( 2 );
41 
42         // Then - expected exception thrown
43     }
44 
45     @Test
shouldFailWhenMethodCalledTooFewTimes()46     public void shouldFailWhenMethodCalledTooFewTimes(){
47         // Given
48         mockOne.oneArg( 1 );
49         mockOne.oneArg( 2 );
50 
51         InOrder verifier = inOrder( mockOne );
52         verifier.verify( mockOne, calls(1)).oneArg( 1 );
53 
54         exceptionRule.expect( VerificationInOrderFailure.class );
55         exceptionRule.expectMessage( "Verification in order failure" );
56         exceptionRule.expectMessage( "mockOne.oneArg(2)" );
57         exceptionRule.expectMessage( "Wanted 2 times" );
58         exceptionRule.expectMessage( "But was 1 time" );
59 
60         // When
61         verifier.verify( mockOne, calls(2)).oneArg( 2 );
62 
63         // Then - expected exception thrown
64     }
65 
66     @Test
shouldFailWhenSingleMethodCallsAreOutOfSequence()67     public void shouldFailWhenSingleMethodCallsAreOutOfSequence(){
68         // Given
69         mockOne.oneArg( 1 );
70         mockOne.oneArg( 2 );
71 
72         InOrder verifier = inOrder( mockOne );
73         verifier.verify( mockOne, calls(1)).oneArg( 2 );
74 
75         exceptionRule.expect( VerificationInOrderFailure.class );
76         exceptionRule.expectMessage( "Verification in order failure" );
77         exceptionRule.expectMessage( "Wanted but not invoked" );
78         exceptionRule.expectMessage( "mockOne.oneArg(1)" );
79 
80         // When
81         verifier.verify( mockOne, calls(1)).oneArg( 1 );
82 
83         // Then - expected exception thrown
84     }
85 
86     @Test
shouldFailWhenDifferentMethodCallsAreOutOfSequence()87     public void shouldFailWhenDifferentMethodCallsAreOutOfSequence(){
88         // Given
89         mockOne.oneArg( 1 );
90         mockOne.voidMethod();
91 
92         InOrder verifier = inOrder( mockOne );
93         verifier.verify( mockOne, calls(1)).voidMethod();
94 
95         exceptionRule.expect( VerificationInOrderFailure.class );
96         exceptionRule.expectMessage( "Verification in order failure" );
97         exceptionRule.expectMessage( "Wanted but not invoked" );
98         exceptionRule.expectMessage( "mockOne.oneArg(1)" );
99 
100         // When
101         verifier.verify( mockOne, calls(1)).oneArg( 1 );
102 
103         // Then - expected exception thrown
104     }
105 
106     @Test
shouldFailWhenMethodCallsOnDifferentMocksAreOutOfSequence()107     public void shouldFailWhenMethodCallsOnDifferentMocksAreOutOfSequence(){
108         // Given
109         mockOne.voidMethod();
110         mockTwo.voidMethod();
111 
112         InOrder verifier = inOrder( mockOne, mockTwo );
113         verifier.verify( mockTwo, calls(1)).voidMethod();
114 
115         exceptionRule.expect( VerificationInOrderFailure.class );
116         exceptionRule.expectMessage( "Verification in order failure" );
117         exceptionRule.expectMessage( "Wanted but not invoked" );
118         exceptionRule.expectMessage( "mockOne.voidMethod()" );
119 
120         // When
121         verifier.verify( mockOne, calls(1)).voidMethod();
122 
123         // Then - expected exception thrown
124     }
125 
126 
127     @Test
shouldAllowSequentialCallsToCallsForSingleMethod()128     public void shouldAllowSequentialCallsToCallsForSingleMethod(){
129         // Given
130         mockOne.oneArg( 1 );
131         mockOne.oneArg( 2 );
132         mockOne.oneArg( 2 );
133         mockOne.oneArg( 1 );
134 
135         InOrder verifier = inOrder( mockOne );
136 
137         // When
138         verifier.verify( mockOne, calls(1)).oneArg( 1 );
139         verifier.verify( mockOne, calls(2)).oneArg( 2 );
140         verifier.verify( mockOne, calls(1)).oneArg( 1 );
141         verifyNoMoreInteractions(mockOne);
142         verifier.verifyNoMoreInteractions();
143 
144         // Then - no exception thrown
145     }
146 
147     @Test
shouldAllowSequentialCallsToCallsForDifferentMethods()148     public void shouldAllowSequentialCallsToCallsForDifferentMethods(){
149         // Given
150         mockOne.oneArg( 1 );
151         mockOne.voidMethod();
152         mockOne.voidMethod();
153         mockOne.oneArg( 1 );
154 
155         InOrder verifier = inOrder( mockOne );
156 
157         // When
158         verifier.verify( mockOne, calls(1)).oneArg( 1 );
159         verifier.verify( mockOne, calls(2)).voidMethod();
160         verifier.verify( mockOne, calls(1)).oneArg(1);
161         verifyNoMoreInteractions(mockOne);
162         verifier.verifyNoMoreInteractions();
163 
164         // Then - no exception thrown
165     }
166 
167     @Test
shouldAllowSequentialCallsToCallsForMethodsOnDifferentMocks()168     public void shouldAllowSequentialCallsToCallsForMethodsOnDifferentMocks(){
169         // Given
170         mockOne.voidMethod();
171         mockTwo.voidMethod();
172         mockTwo.voidMethod();
173         mockOne.voidMethod();
174 
175         InOrder verifier = inOrder( mockOne, mockTwo );
176 
177         // When
178         verifier.verify( mockOne, calls(1)).voidMethod();
179         verifier.verify( mockTwo, calls(2)).voidMethod();
180         verifier.verify( mockOne, calls(1)).voidMethod();
181         verifyNoMoreInteractions(mockOne);
182         verifyNoMoreInteractions(mockTwo);
183         verifier.verifyNoMoreInteractions();
184 
185         // Then - no exception thrown
186     }
187 
188 
189     @Test
shouldAllowFewerCallsForSingleMethod()190     public void shouldAllowFewerCallsForSingleMethod(){
191         // Given
192         mockOne.oneArg( 1 );
193         mockOne.oneArg( 2 );
194         mockOne.oneArg( 2 );
195         mockOne.oneArg( 1 );
196         mockOne.oneArg( 2 );
197 
198         InOrder verifier = inOrder( mockOne );
199 
200         // When
201         verifier.verify( mockOne, calls(1)).oneArg( 1 );
202         verifier.verify( mockOne, calls(1)).oneArg( 2 );
203         verifier.verify( mockOne, calls(1)).oneArg( 1 );
204         verifier.verify( mockOne, calls(1)).oneArg( 2 );
205 
206         // Then - no exception thrown
207     }
208 
209     @Test
shouldNotVerifySkippedCallsWhenFewerCallsForSingleMethod()210     public void shouldNotVerifySkippedCallsWhenFewerCallsForSingleMethod(){
211         // Given
212         mockOne.oneArg( 1 );
213         mockOne.oneArg( 2 );
214         mockOne.oneArg( 2 );
215         mockOne.oneArg( 1 );
216 
217         InOrder verifier = inOrder( mockOne );
218         verifier.verify( mockOne, calls(1)).oneArg( 1 );
219         verifier.verify( mockOne, calls(1)).oneArg( 2 );
220         verifier.verify( mockOne, calls(1)).oneArg( 1 );
221 
222         exceptionRule.expect( NoInteractionsWanted.class );
223 
224         // When
225         verifyNoMoreInteractions( mockOne );
226 
227         // Then - expected exception thrown
228     }
229 
230     @Test
shouldNotVerifySkippedCallsInInOrderWhenFewerCallsForSingleMethod()231     public void shouldNotVerifySkippedCallsInInOrderWhenFewerCallsForSingleMethod(){
232         // Given
233         mockOne.oneArg( 1 );
234         mockOne.oneArg( 2 );
235         mockOne.oneArg( 2 );
236 
237         InOrder verifier = inOrder( mockOne );
238         verifier.verify( mockOne, calls(1)).oneArg( 1 );
239         verifier.verify( mockOne, calls(1)).oneArg( 2 );
240 
241         exceptionRule.expect( VerificationInOrderFailure.class );
242         exceptionRule.expectMessage( "No interactions wanted here" );
243 
244         // When
245         verifier.verifyNoMoreInteractions();
246 
247         // Then - expected exception thrown
248     }
249 
250     @Test
shouldAllowFewerCallsForDifferentMethods()251     public void shouldAllowFewerCallsForDifferentMethods(){
252         // Given
253         mockOne.oneArg( 1 );
254         mockOne.voidMethod();
255         mockOne.voidMethod();
256         mockOne.oneArg( 1 );
257         mockOne.voidMethod();
258 
259         InOrder verifier = inOrder( mockOne );
260 
261         // When
262         verifier.verify( mockOne, calls(1)).oneArg( 1 );
263         verifier.verify( mockOne, calls(1)).voidMethod();
264         verifier.verify( mockOne, calls(1)).oneArg( 1 );
265         verifier.verify( mockOne, calls(1)).voidMethod();
266 
267         // Then - no exception thrown
268     }
269 
270     @Test
shouldNotVerifySkippedCallsWhenFewerCallsForDifferentMethods()271     public void shouldNotVerifySkippedCallsWhenFewerCallsForDifferentMethods(){
272         // Given
273         mockOne.oneArg( 1 );
274         mockOne.voidMethod();
275         mockOne.voidMethod();
276         mockOne.oneArg( 1 );
277 
278         InOrder verifier = inOrder( mockOne );
279         verifier.verify( mockOne, calls(1)).oneArg( 1 );
280         verifier.verify( mockOne, calls(1)).voidMethod();
281         verifier.verify( mockOne, calls(1)).oneArg( 1 );
282 
283         exceptionRule.expect( NoInteractionsWanted.class );
284 
285         // When
286         verifyNoMoreInteractions( mockOne );
287 
288         // Then - no exception thrown
289     }
290 
291     @Test
shouldNotVerifySkippedCallsInInOrderWhenFewerCallsForDifferentMethods()292     public void shouldNotVerifySkippedCallsInInOrderWhenFewerCallsForDifferentMethods(){
293         // Given
294         mockOne.oneArg( 1 );
295         mockOne.voidMethod();
296         mockOne.voidMethod();
297 
298         InOrder verifier = inOrder( mockOne );
299         verifier.verify( mockOne, calls(1)).oneArg( 1 );
300         verifier.verify( mockOne, calls(1)).voidMethod();
301 
302         exceptionRule.expect( VerificationInOrderFailure.class );
303         exceptionRule.expectMessage( "No interactions wanted here" );
304 
305         // When
306         verifier.verifyNoMoreInteractions();
307 
308         // Then - expected exception thrown
309     }
310 
311     @Test
shouldAllowFewerCallsForMethodsOnDifferentMocks()312     public void shouldAllowFewerCallsForMethodsOnDifferentMocks(){
313         // Given
314         mockOne.voidMethod();
315         mockTwo.voidMethod();
316         mockTwo.voidMethod();
317         mockOne.voidMethod();
318         mockTwo.voidMethod();
319 
320         InOrder verifier = inOrder( mockOne, mockTwo );
321 
322         // When
323         verifier.verify( mockOne, calls(1)).voidMethod();
324         verifier.verify( mockTwo, calls(1)).voidMethod();
325         verifier.verify( mockOne, calls(1)).voidMethod();
326         verifier.verify( mockTwo, calls(1)).voidMethod();
327 
328         // Then - no exception thrown
329     }
330 
331     @Test
shouldNotVerifySkippedCallsWhenFewerCallsForMethodsOnDifferentMocks()332     public void shouldNotVerifySkippedCallsWhenFewerCallsForMethodsOnDifferentMocks(){
333         // Given
334         mockOne.voidMethod();
335         mockTwo.voidMethod();
336         mockTwo.voidMethod();
337         mockOne.voidMethod();
338 
339         InOrder verifier = inOrder( mockOne, mockTwo );
340         verifier.verify( mockOne, calls(1)).voidMethod();
341         verifier.verify( mockTwo, calls(1)).voidMethod();
342         verifier.verify( mockOne, calls(1)).voidMethod();
343 
344         exceptionRule.expect(NoInteractionsWanted.class);
345 
346         // When
347         verifyNoMoreInteractions( mockTwo );
348 
349         // Then - expected exception thrown
350     }
351 
352     @Test
shouldNotVerifySkippedCallsInInOrderWhenFewerCallsForMethodsOnDifferentMocks()353     public void shouldNotVerifySkippedCallsInInOrderWhenFewerCallsForMethodsOnDifferentMocks(){
354         // Given
355         mockOne.voidMethod();
356         mockTwo.voidMethod();
357         mockTwo.voidMethod();
358 
359         InOrder verifier = inOrder( mockOne, mockTwo );
360         verifier.verify( mockOne, calls(1)).voidMethod();
361         verifier.verify( mockTwo, calls(1)).voidMethod();
362 
363         exceptionRule.expect( VerificationInOrderFailure.class );
364         exceptionRule.expectMessage( "No interactions wanted here" );
365 
366         // When
367         verifier.verifyNoMoreInteractions();
368 
369         // Then - expected exception thrown
370     }
371 
372     @Test
shouldVerifyWithCallsAfterUseOfTimes()373     public void shouldVerifyWithCallsAfterUseOfTimes(){
374         // Given
375         mockOne.oneArg( 1 );
376         mockOne.oneArg( 2 );
377         mockOne.oneArg( 2 );
378         mockOne.oneArg( 1 );
379 
380         InOrder verifier = inOrder( mockOne );
381 
382         // When
383         verifier.verify( mockOne, times(1)).oneArg( 1 );
384         verifier.verify( mockOne, calls(2)).oneArg( 2 );
385         verifier.verify( mockOne, calls(1)).oneArg( 1 );
386 
387         // Then - no exception thrown
388     }
389 
390     @Test
shouldVerifyWithCallsAfterUseOfAtLeast()391     public void shouldVerifyWithCallsAfterUseOfAtLeast(){
392         // Given
393         mockOne.oneArg( 1 );
394         mockOne.oneArg( 2 );
395         mockOne.oneArg( 2 );
396 
397         InOrder verifier = inOrder( mockOne );
398 
399         // When
400         verifier.verify( mockOne, atLeast(1)).oneArg( 1 );
401         verifier.verify( mockOne, calls(2)).oneArg( 2 );
402 
403         // Then - no exception thrown
404     }
405 
406     @Test
shouldVerifyWithTimesAfterUseOfCalls()407     public void shouldVerifyWithTimesAfterUseOfCalls(){
408         // Given
409         mockOne.oneArg( 1 );
410         mockOne.oneArg( 2 );
411         mockOne.oneArg( 2 );
412         mockOne.oneArg( 1 );
413 
414         InOrder verifier = inOrder( mockOne );
415 
416         // When
417         verifier.verify( mockOne, calls(1)).oneArg( 1 );
418         verifier.verify( mockOne, times(2)).oneArg( 2 );
419         verifier.verify( mockOne, times(1)).oneArg( 1 );
420 
421         // Then - no exception thrown
422     }
423 
424     @Test
shouldVerifyWithAtLeastAfterUseOfCalls()425     public void shouldVerifyWithAtLeastAfterUseOfCalls(){
426         // Given
427         mockOne.oneArg( 1 );
428         mockOne.oneArg( 2 );
429         mockOne.oneArg( 2 );
430         mockOne.oneArg( 1 );
431 
432         InOrder verifier = inOrder( mockOne );
433 
434         // When
435         verifier.verify( mockOne, calls(1)).oneArg( 1 );
436         verifier.verify( mockOne, atLeast(1)).oneArg( 2 );
437         verifier.verify( mockOne, atLeast(1)).oneArg( 1 );
438 
439         // Then - no exception thrown
440     }
441 
442     @Test
shouldVerifyWithTimesAfterCallsInSameChunk()443     public void shouldVerifyWithTimesAfterCallsInSameChunk(){
444         // Given
445         mockOne.oneArg( 1 );
446         mockOne.oneArg( 1 );
447         mockOne.oneArg( 1 );
448 
449         InOrder verifier = inOrder( mockOne );
450 
451         // When
452         verifier.verify( mockOne, calls(1)).oneArg( 1 );
453         verifier.verify( mockOne, times(2)).oneArg( 1 );
454         verifier.verifyNoMoreInteractions();
455 
456         // Then - no exception thrown
457     }
458 
459     @Test
shouldFailToCreateCallsWithZeroArgument()460     public void shouldFailToCreateCallsWithZeroArgument(){
461         // Given
462         InOrder verifier = inOrder( mockOne );
463         exceptionRule.expect( MockitoException.class );
464         exceptionRule.expectMessage( "Negative and zero values are not allowed here" );
465 
466         // When
467         verifier.verify( mockOne, calls(0)).voidMethod();
468 
469         // Then - expected exception thrown
470     }
471 
472     @Test
shouldFailToCreateCallsWithNegativeArgument()473     public void shouldFailToCreateCallsWithNegativeArgument(){
474         // Given
475         InOrder verifier = inOrder( mockOne );
476         exceptionRule.expect( MockitoException.class );
477         exceptionRule.expectMessage( "Negative and zero values are not allowed here" );
478 
479         // When
480         verifier.verify( mockOne, calls(-1)).voidMethod();
481 
482         // Then - expected exception thrown
483     }
484 
485     @Test
shouldFailToCreateCallsForNonInOrderVerification()486     public void shouldFailToCreateCallsForNonInOrderVerification(){
487         // Given
488         mockOne.voidMethod();
489         exceptionRule.expect( MockitoException.class );
490         exceptionRule.expectMessage( "calls is only intended to work with InOrder" );
491 
492         // When
493         verify( mockOne, calls(1)).voidMethod();
494 
495         // Then - expected exception thrown
496     }
497 }
498