• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static org.assertj.core.api.Assertions.assertThat;
8 import static org.assertj.core.api.Assertions.assertThatThrownBy;
9 import static org.junit.Assert.fail;
10 import static org.mockito.Mockito.atLeastOnce;
11 import static org.mockito.Mockito.inOrder;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verifyNoInteractions;
15 import static org.mockito.Mockito.verifyNoMoreInteractions;
16 
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.InOrder;
20 import org.mockito.exceptions.base.MockitoException;
21 import org.mockito.exceptions.misusing.NotAMockException;
22 import org.mockito.exceptions.misusing.NullInsteadOfMockException;
23 import org.mockito.exceptions.verification.NoInteractionsWanted;
24 import org.mockito.exceptions.verification.VerificationInOrderFailure;
25 import org.mockito.exceptions.verification.WantedButNotInvoked;
26 import org.mockito.exceptions.verification.opentest4j.ArgumentsAreDifferent;
27 import org.mockitousage.IMethods;
28 import org.mockitoutil.TestBase;
29 
30 public class BasicVerificationInOrderTest extends TestBase {
31 
32     private IMethods mockOne;
33     private IMethods mockTwo;
34     private IMethods mockThree;
35     private InOrder inOrder;
36 
37     @Before
setUp()38     public void setUp() {
39         mockOne = mock(IMethods.class);
40         mockTwo = mock(IMethods.class);
41         mockThree = mock(IMethods.class);
42 
43         inOrder = inOrder(mockOne, mockTwo, mockThree);
44 
45         mockOne.simpleMethod(1);
46         mockTwo.simpleMethod(2);
47         mockTwo.simpleMethod(2);
48         mockThree.simpleMethod(3);
49         mockTwo.simpleMethod(2);
50         mockOne.simpleMethod(4);
51     }
52 
53     @Test
shouldVerifyInOrder()54     public void shouldVerifyInOrder() {
55         inOrder.verify(mockOne).simpleMethod(1);
56         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
57         inOrder.verify(mockThree).simpleMethod(3);
58         inOrder.verify(mockTwo).simpleMethod(2);
59         inOrder.verify(mockOne).simpleMethod(4);
60         verifyNoMoreInteractions(mockOne, mockTwo, mockThree);
61     }
62 
63     @Test
shouldVerifyInOrderUsingAtLeastOnce()64     public void shouldVerifyInOrderUsingAtLeastOnce() {
65         inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1);
66         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
67         inOrder.verify(mockThree).simpleMethod(3);
68         inOrder.verify(mockTwo).simpleMethod(2);
69         inOrder.verify(mockOne, atLeastOnce()).simpleMethod(4);
70         verifyNoMoreInteractions(mockOne, mockTwo, mockThree);
71     }
72 
73     @Test
shouldVerifyInOrderWhenExpectingSomeInvocationsToBeCalledZeroTimes()74     public void shouldVerifyInOrderWhenExpectingSomeInvocationsToBeCalledZeroTimes() {
75         inOrder.verify(mockOne, times(0)).oneArg(false);
76         inOrder.verify(mockOne).simpleMethod(1);
77         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
78         inOrder.verify(mockTwo, times(0)).simpleMethod(22);
79         inOrder.verify(mockThree).simpleMethod(3);
80         inOrder.verify(mockTwo).simpleMethod(2);
81         inOrder.verify(mockOne).simpleMethod(4);
82         inOrder.verify(mockThree, times(0)).oneArg(false);
83         verifyNoMoreInteractions(mockOne, mockTwo, mockThree);
84     }
85 
86     @Test
shouldFailWhenFirstMockCalledTwice()87     public void shouldFailWhenFirstMockCalledTwice() {
88         inOrder.verify(mockOne).simpleMethod(1);
89         try {
90             inOrder.verify(mockOne).simpleMethod(1);
91             fail();
92         } catch (VerificationInOrderFailure e) {
93         }
94     }
95 
96     @Test
shouldFailWhenLastMockCalledTwice()97     public void shouldFailWhenLastMockCalledTwice() {
98         inOrder.verify(mockOne).simpleMethod(1);
99         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
100         inOrder.verify(mockThree).simpleMethod(3);
101         inOrder.verify(mockTwo).simpleMethod(2);
102         inOrder.verify(mockOne).simpleMethod(4);
103         try {
104             inOrder.verify(mockOne).simpleMethod(4);
105             fail();
106         } catch (VerificationInOrderFailure e) {
107         }
108     }
109 
110     @Test
shouldFailOnFirstMethodBecauseOneInvocationWanted()111     public void shouldFailOnFirstMethodBecauseOneInvocationWanted() {
112         assertThatThrownBy(
113                         () -> {
114                             inOrder.verify(mockOne, times(0)).simpleMethod(1);
115                         })
116                 .isInstanceOf(VerificationInOrderFailure.class)
117                 .hasMessageContainingAll(
118                         "Verification in order failure:",
119                         "iMethods.simpleMethod(1);",
120                         "Wanted 0 times:",
121                         "-> at ",
122                         "But was 1 time:",
123                         "-> at ");
124     }
125 
126     @Test
shouldFailOnFirstMethodBecauseOneInvocationWantedAgain()127     public void shouldFailOnFirstMethodBecauseOneInvocationWantedAgain() {
128         assertThatThrownBy(
129                         () -> {
130                             inOrder.verify(mockOne, times(2)).simpleMethod(1);
131                         })
132                 .isInstanceOf(VerificationInOrderFailure.class)
133                 .hasMessageContainingAll(
134                         "Verification in order failure:",
135                         "iMethods.simpleMethod(1);",
136                         "Wanted 2 times:",
137                         "-> at ",
138                         "But was 1 time:",
139                         "-> at ");
140     }
141 
142     @Test
shouldFailOnSecondMethodBecauseFourInvocationsWanted()143     public void shouldFailOnSecondMethodBecauseFourInvocationsWanted() {
144         inOrder.verify(mockOne, times(1)).simpleMethod(1);
145         try {
146             inOrder.verify(mockTwo, times(4)).simpleMethod(2);
147             fail();
148         } catch (VerificationInOrderFailure e) {
149         }
150     }
151 
152     @Test
shouldFailOnSecondMethodBecauseTwoInvocationsWantedAgain()153     public void shouldFailOnSecondMethodBecauseTwoInvocationsWantedAgain() {
154         inOrder.verify(mockOne, times(1)).simpleMethod(1);
155         try {
156             inOrder.verify(mockTwo, times(0)).simpleMethod(2);
157             fail();
158         } catch (VerificationInOrderFailure e) {
159         }
160     }
161 
162     @Test
shouldFailOnLastMethodBecauseOneInvocationWanted()163     public void shouldFailOnLastMethodBecauseOneInvocationWanted() {
164         inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1);
165         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
166         inOrder.verify(mockThree, atLeastOnce()).simpleMethod(3);
167         inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
168         try {
169             inOrder.verify(mockOne, times(0)).simpleMethod(4);
170             fail();
171         } catch (VerificationInOrderFailure e) {
172         }
173     }
174 
175     @Test
shouldFailOnLastMethodBecauseOneInvocationWantedAgain()176     public void shouldFailOnLastMethodBecauseOneInvocationWantedAgain() {
177         inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1);
178         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
179         inOrder.verify(mockThree, atLeastOnce()).simpleMethod(3);
180         inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
181         try {
182             inOrder.verify(mockOne, times(2)).simpleMethod(4);
183             fail();
184         } catch (VerificationInOrderFailure e) {
185         }
186     }
187 
188     /* ------------- */
189 
190     @Test
shouldFailOnFirstMethodBecauseDifferentArgsWanted()191     public void shouldFailOnFirstMethodBecauseDifferentArgsWanted() {
192         assertThatThrownBy(
193                         () -> {
194                             inOrder.verify(mockOne).simpleMethod(100);
195                         })
196                 .isInstanceOf(ArgumentsAreDifferent.class)
197                 .hasMessageContainingAll(
198                         "Argument(s) are different! Wanted:",
199                         "iMethods.simpleMethod(100);",
200                         "-> at ",
201                         "Actual invocations have different arguments:",
202                         "iMethods.simpleMethod(1);",
203                         "-> at ",
204                         "iMethods.simpleMethod(2);",
205                         "-> at ",
206                         "iMethods.simpleMethod(2);",
207                         "-> at ",
208                         "iMethods.simpleMethod(3);",
209                         "-> at ",
210                         "iMethods.simpleMethod(2);",
211                         "-> at ",
212                         "iMethods.simpleMethod(4);",
213                         "-> at ");
214     }
215 
216     @Test
shouldFailOnFirstMethodBecauseDifferentMethodWanted()217     public void shouldFailOnFirstMethodBecauseDifferentMethodWanted() {
218         assertThatThrownBy(
219                         () -> {
220                             inOrder.verify(mockOne).oneArg(true);
221                         })
222                 .isInstanceOf(WantedButNotInvoked.class)
223                 .hasMessageContainingAll(
224                         "Wanted but not invoked:",
225                         "iMethods.oneArg(true);",
226                         "-> at ",
227                         "However, there were exactly 6 interactions with this mock:",
228                         "iMethods.simpleMethod(1);",
229                         "-> at ",
230                         "iMethods.simpleMethod(2);",
231                         "-> at ",
232                         "iMethods.simpleMethod(2);",
233                         "-> at ",
234                         "iMethods.simpleMethod(3);",
235                         "-> at ",
236                         "iMethods.simpleMethod(2);",
237                         "-> at ",
238                         "iMethods.simpleMethod(4);",
239                         "-> at ");
240     }
241 
242     @Test
shouldFailOnSecondMethodBecauseDifferentArgsWanted()243     public void shouldFailOnSecondMethodBecauseDifferentArgsWanted() {
244         inOrder.verify(mockOne).simpleMethod(1);
245         try {
246             inOrder.verify(mockTwo, times(2)).simpleMethod(-999);
247             fail();
248         } catch (VerificationInOrderFailure e) {
249         }
250     }
251 
252     @Test
shouldFailOnSecondMethodBecauseDifferentMethodWanted()253     public void shouldFailOnSecondMethodBecauseDifferentMethodWanted() {
254         inOrder.verify(mockOne, times(1)).simpleMethod(1);
255         try {
256             inOrder.verify(mockTwo, times(2)).oneArg(true);
257             fail();
258         } catch (VerificationInOrderFailure e) {
259         }
260     }
261 
262     @Test
shouldFailOnLastMethodBecauseDifferentArgsWanted()263     public void shouldFailOnLastMethodBecauseDifferentArgsWanted() {
264         inOrder.verify(mockOne).simpleMethod(1);
265         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
266         inOrder.verify(mockThree).simpleMethod(3);
267         inOrder.verify(mockTwo).simpleMethod(2);
268         try {
269             inOrder.verify(mockOne).simpleMethod(-666);
270             fail();
271         } catch (VerificationInOrderFailure e) {
272         }
273     }
274 
275     @Test
shouldFailOnLastMethodBecauseDifferentMethodWanted()276     public void shouldFailOnLastMethodBecauseDifferentMethodWanted() {
277         inOrder.verify(mockOne).simpleMethod(1);
278         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
279         inOrder.verify(mockThree).simpleMethod(3);
280         inOrder.verify(mockTwo).simpleMethod(2);
281         try {
282             inOrder.verify(mockOne).oneArg(false);
283             fail();
284         } catch (VerificationInOrderFailure e) {
285         }
286     }
287 
288     /* -------------- */
289 
290     @Test
shouldFailWhenLastMethodVerifiedFirst()291     public void shouldFailWhenLastMethodVerifiedFirst() {
292         inOrder.verify(mockOne).simpleMethod(4);
293         try {
294             inOrder.verify(mockOne).simpleMethod(1);
295             fail();
296         } catch (VerificationInOrderFailure e) {
297         }
298     }
299 
300     @Test
shouldFailWhenMiddleMethodVerifiedFirst()301     public void shouldFailWhenMiddleMethodVerifiedFirst() {
302         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
303         try {
304             inOrder.verify(mockOne).simpleMethod(1);
305             fail();
306         } catch (VerificationInOrderFailure e) {
307         }
308     }
309 
310     @Test
shouldFailWhenMiddleMethodVerifiedFirstInAtLeastOnceMode()311     public void shouldFailWhenMiddleMethodVerifiedFirstInAtLeastOnceMode() {
312         inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
313         try {
314             inOrder.verify(mockOne).simpleMethod(1);
315             fail();
316         } catch (VerificationInOrderFailure e) {
317         }
318     }
319 
320     @Test
shouldFailOnVerifyNoMoreInteractions()321     public void shouldFailOnVerifyNoMoreInteractions() {
322         inOrder.verify(mockOne).simpleMethod(1);
323         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
324         inOrder.verify(mockThree).simpleMethod(3);
325         inOrder.verify(mockTwo).simpleMethod(2);
326 
327         try {
328             verifyNoMoreInteractions(mockOne, mockTwo, mockThree);
329             fail();
330         } catch (NoInteractionsWanted e) {
331         }
332     }
333 
334     @Test
shouldFailOnVerifyNoInteractions()335     public void shouldFailOnVerifyNoInteractions() {
336         assertThatThrownBy(
337                         () -> {
338                             verifyNoInteractions(mockOne);
339                         })
340                 .isInstanceOf(NoInteractionsWanted.class)
341                 .hasMessageContainingAll(
342                         "No interactions wanted here:",
343                         "-> at ",
344                         "But found these interactions on mock 'iMethods':",
345                         "-> at ",
346                         "-> at ",
347                         "***",
348                         "For your reference, here is the list of all invocations ([?] - means unverified).",
349                         "1. [?]-> at ",
350                         "2. [?]-> at ");
351     }
352 
353     @SuppressWarnings({"all", "CheckReturnValue", "MockitoUsage"})
354     @Test
shouldScreamWhenNullPassed()355     public void shouldScreamWhenNullPassed() {
356         assertThatThrownBy(
357                         () -> {
358                             inOrder((Object[]) null);
359                         })
360                 .isInstanceOf(MockitoException.class)
361                 .hasMessageContainingAll(
362                         "Method requires argument(s)!",
363                         "Pass mocks that require verification in order.",
364                         "For example:",
365                         "    InOrder inOrder = inOrder(mockOne, mockTwo);");
366     }
367 
368     @Test
shouldThrowNullPassedToVerifyException()369     public void shouldThrowNullPassedToVerifyException() {
370         try {
371             inOrder.verify(null);
372             fail();
373         } catch (NullInsteadOfMockException e) {
374             assertThat(e)
375                     .hasMessageContaining(
376                             "Argument passed to verify() should be a mock but is null!");
377         }
378     }
379 
380     @Test
shouldThrowNotAMockPassedToVerifyException()381     public void shouldThrowNotAMockPassedToVerifyException() {
382         Object object = new Object();
383         try {
384             inOrder.verify(object);
385             fail();
386         } catch (NotAMockException e) {
387             assertThat(e)
388                     .hasMessageContaining(
389                             "Argument passed to verify() is of type Object and is not a mock!");
390         }
391     }
392 }
393