• 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 
6 package org.mockitousage.verification;
7 
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.mockito.InOrder;
11 import org.mockito.exceptions.base.MockitoException;
12 import org.mockito.exceptions.verification.NoInteractionsWanted;
13 import org.mockito.exceptions.verification.VerificationInOrderFailure;
14 import org.mockito.exceptions.verification.WantedButNotInvoked;
15 import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
16 import org.mockitousage.IMethods;
17 import org.mockitoutil.TestBase;
18 
19 import static junit.framework.TestCase.fail;
20 import static org.mockito.Mockito.*;
21 
22 public class BasicVerificationInOrderTest extends TestBase {
23 
24     private IMethods mockOne;
25     private IMethods mockTwo;
26     private IMethods mockThree;
27     private InOrder inOrder;
28 
29     @Before
setUp()30     public void setUp() {
31         mockOne = mock(IMethods.class);
32         mockTwo = mock(IMethods.class);
33         mockThree = mock(IMethods.class);
34 
35         inOrder = inOrder(mockOne, mockTwo, mockThree);
36 
37         mockOne.simpleMethod(1);
38         mockTwo.simpleMethod(2);
39         mockTwo.simpleMethod(2);
40         mockThree.simpleMethod(3);
41         mockTwo.simpleMethod(2);
42         mockOne.simpleMethod(4);
43     }
44 
45     @Test
shouldVerifyInOrder()46     public void shouldVerifyInOrder() {
47         inOrder.verify(mockOne).simpleMethod(1);
48         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
49         inOrder.verify(mockThree).simpleMethod(3);
50         inOrder.verify(mockTwo).simpleMethod(2);
51         inOrder.verify(mockOne).simpleMethod(4);
52         verifyNoMoreInteractions(mockOne, mockTwo, mockThree);
53     }
54 
55     @Test
shouldVerifyInOrderUsingAtLeastOnce()56     public void shouldVerifyInOrderUsingAtLeastOnce() {
57         inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1);
58         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
59         inOrder.verify(mockThree).simpleMethod(3);
60         inOrder.verify(mockTwo).simpleMethod(2);
61         inOrder.verify(mockOne, atLeastOnce()).simpleMethod(4);
62         verifyNoMoreInteractions(mockOne, mockTwo, mockThree);
63     }
64 
65     @Test
shouldVerifyInOrderWhenExpectingSomeInvocationsToBeCalledZeroTimes()66     public void shouldVerifyInOrderWhenExpectingSomeInvocationsToBeCalledZeroTimes() {
67         inOrder.verify(mockOne, times(0)).oneArg(false);
68         inOrder.verify(mockOne).simpleMethod(1);
69         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
70         inOrder.verify(mockTwo, times(0)).simpleMethod(22);
71         inOrder.verify(mockThree).simpleMethod(3);
72         inOrder.verify(mockTwo).simpleMethod(2);
73         inOrder.verify(mockOne).simpleMethod(4);
74         inOrder.verify(mockThree, times(0)).oneArg(false);
75         verifyNoMoreInteractions(mockOne, mockTwo, mockThree);
76     }
77 
78     @Test
shouldFailWhenFirstMockCalledTwice()79     public void shouldFailWhenFirstMockCalledTwice() {
80         inOrder.verify(mockOne).simpleMethod(1);
81         try {
82             inOrder.verify(mockOne).simpleMethod(1);
83             fail();
84         } catch (VerificationInOrderFailure e) {
85         }
86     }
87 
88     @Test
shouldFailWhenLastMockCalledTwice()89     public void shouldFailWhenLastMockCalledTwice() {
90         inOrder.verify(mockOne).simpleMethod(1);
91         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
92         inOrder.verify(mockThree).simpleMethod(3);
93         inOrder.verify(mockTwo).simpleMethod(2);
94         inOrder.verify(mockOne).simpleMethod(4);
95         try {
96             inOrder.verify(mockOne).simpleMethod(4);
97             fail();
98         } catch (VerificationInOrderFailure e) {
99         }
100     }
101 
102     @Test(expected = VerificationInOrderFailure.class)
shouldFailOnFirstMethodBecauseOneInvocationWanted()103     public void shouldFailOnFirstMethodBecauseOneInvocationWanted() {
104         inOrder.verify(mockOne, times(0)).simpleMethod(1);
105     }
106 
107     @Test(expected = VerificationInOrderFailure.class)
shouldFailOnFirstMethodBecauseOneInvocationWantedAgain()108     public void shouldFailOnFirstMethodBecauseOneInvocationWantedAgain() {
109         inOrder.verify(mockOne, times(2)).simpleMethod(1);
110     }
111 
112     @Test
shouldFailOnSecondMethodBecauseFourInvocationsWanted()113     public void shouldFailOnSecondMethodBecauseFourInvocationsWanted() {
114         inOrder.verify(mockOne, times(1)).simpleMethod(1);
115         try {
116             inOrder.verify(mockTwo, times(4)).simpleMethod(2);
117             fail();
118         } catch (VerificationInOrderFailure e) {
119         }
120     }
121 
122     @Test
shouldFailOnSecondMethodBecauseTwoInvocationsWantedAgain()123     public void shouldFailOnSecondMethodBecauseTwoInvocationsWantedAgain() {
124         inOrder.verify(mockOne, times(1)).simpleMethod(1);
125         try {
126             inOrder.verify(mockTwo, times(0)).simpleMethod(2);
127             fail();
128         } catch (VerificationInOrderFailure e) {
129         }
130     }
131 
132     @Test
shouldFailOnLastMethodBecauseOneInvocationWanted()133     public void shouldFailOnLastMethodBecauseOneInvocationWanted() {
134         inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1);
135         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
136         inOrder.verify(mockThree, atLeastOnce()).simpleMethod(3);
137         inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
138         try {
139             inOrder.verify(mockOne, times(0)).simpleMethod(4);
140             fail();
141         } catch (VerificationInOrderFailure e) {
142         }
143     }
144 
145     @Test
shouldFailOnLastMethodBecauseOneInvocationWantedAgain()146     public void shouldFailOnLastMethodBecauseOneInvocationWantedAgain() {
147         inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1);
148         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
149         inOrder.verify(mockThree, atLeastOnce()).simpleMethod(3);
150         inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
151         try {
152             inOrder.verify(mockOne, times(2)).simpleMethod(4);
153             fail();
154         } catch (VerificationInOrderFailure e) {
155         }
156     }
157 
158     /* ------------- */
159 
160     @Test(expected = ArgumentsAreDifferent.class)
shouldFailOnFirstMethodBecauseDifferentArgsWanted()161     public void shouldFailOnFirstMethodBecauseDifferentArgsWanted() {
162         inOrder.verify(mockOne).simpleMethod(100);
163     }
164 
165     @Test(expected = WantedButNotInvoked.class)
shouldFailOnFirstMethodBecauseDifferentMethodWanted()166     public void shouldFailOnFirstMethodBecauseDifferentMethodWanted() {
167         inOrder.verify(mockOne).oneArg(true);
168     }
169 
170     @Test
shouldFailOnSecondMethodBecauseDifferentArgsWanted()171     public void shouldFailOnSecondMethodBecauseDifferentArgsWanted() {
172         inOrder.verify(mockOne).simpleMethod(1);
173         try {
174             inOrder.verify(mockTwo, times(2)).simpleMethod(-999);
175             fail();
176         } catch (VerificationInOrderFailure e) {
177         }
178     }
179 
180     @Test
shouldFailOnSecondMethodBecauseDifferentMethodWanted()181     public void shouldFailOnSecondMethodBecauseDifferentMethodWanted() {
182         inOrder.verify(mockOne, times(1)).simpleMethod(1);
183         try {
184             inOrder.verify(mockTwo, times(2)).oneArg(true);
185             fail();
186         } catch (VerificationInOrderFailure e) {
187         }
188     }
189 
190     @Test
shouldFailOnLastMethodBecauseDifferentArgsWanted()191     public void shouldFailOnLastMethodBecauseDifferentArgsWanted() {
192         inOrder.verify(mockOne).simpleMethod(1);
193         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
194         inOrder.verify(mockThree).simpleMethod(3);
195         inOrder.verify(mockTwo).simpleMethod(2);
196         try {
197             inOrder.verify(mockOne).simpleMethod(-666);
198             fail();
199         } catch (VerificationInOrderFailure e) {
200         }
201     }
202 
203     @Test
shouldFailOnLastMethodBecauseDifferentMethodWanted()204     public void shouldFailOnLastMethodBecauseDifferentMethodWanted() {
205         inOrder.verify(mockOne).simpleMethod(1);
206         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
207         inOrder.verify(mockThree).simpleMethod(3);
208         inOrder.verify(mockTwo).simpleMethod(2);
209         try {
210             inOrder.verify(mockOne).oneArg(false);
211             fail();
212         } catch (VerificationInOrderFailure e) {
213         }
214     }
215 
216     /* -------------- */
217 
218     @Test
shouldFailWhenLastMethodVerifiedFirst()219     public void shouldFailWhenLastMethodVerifiedFirst() {
220         inOrder.verify(mockOne).simpleMethod(4);
221         try {
222             inOrder.verify(mockOne).simpleMethod(1);
223             fail();
224         } catch (VerificationInOrderFailure e) {
225         }
226     }
227 
228     @Test
shouldFailWhenMiddleMethodVerifiedFirst()229     public void shouldFailWhenMiddleMethodVerifiedFirst() {
230         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
231         try {
232             inOrder.verify(mockOne).simpleMethod(1);
233             fail();
234         } catch (VerificationInOrderFailure e) {
235         }
236     }
237 
238     @Test
shouldFailWhenMiddleMethodVerifiedFirstInAtLeastOnceMode()239     public void shouldFailWhenMiddleMethodVerifiedFirstInAtLeastOnceMode() {
240         inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
241         try {
242             inOrder.verify(mockOne).simpleMethod(1);
243             fail();
244         } catch (VerificationInOrderFailure e) {
245         }
246     }
247 
248     @Test
shouldFailOnVerifyNoMoreInteractions()249     public void shouldFailOnVerifyNoMoreInteractions() {
250         inOrder.verify(mockOne).simpleMethod(1);
251         inOrder.verify(mockTwo, times(2)).simpleMethod(2);
252         inOrder.verify(mockThree).simpleMethod(3);
253         inOrder.verify(mockTwo).simpleMethod(2);
254 
255         try {
256             verifyNoMoreInteractions(mockOne, mockTwo, mockThree);
257             fail();
258         } catch (NoInteractionsWanted e) {
259         }
260     }
261 
262     @Test(expected = NoInteractionsWanted.class)
shouldFailOnVerifyZeroInteractions()263     public void shouldFailOnVerifyZeroInteractions() {
264         verifyZeroInteractions(mockOne);
265     }
266 
267     @SuppressWarnings("all")
268     @Test(expected = MockitoException.class)
shouldScreamWhenNullPassed()269     public void shouldScreamWhenNullPassed() {
270         inOrder((Object[])null);
271     }
272 }
273