1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5
6package org.mockitousage.verification;
7
8import org.junit.Before;
9import org.junit.Test;
10import org.mockito.InOrder;
11import org.mockito.exceptions.verification.NeverWantedButInvoked;
12import org.mockito.exceptions.verification.NoInteractionsWanted;
13import org.mockito.exceptions.verification.VerificationInOrderFailure;
14import org.mockito.exceptions.verification.WantedButNotInvoked;
15import org.mockitousage.IMethods;
16import org.mockitoutil.TestBase;
17
18import static junit.framework.TestCase.fail;
19import static org.mockito.Mockito.*;
20
21/**
22 * ignored since 'relaxed' in order verification is not implemented (too complex to bother, maybe later).
23 */
24public class RelaxedVerificationInOrderTest extends TestBase {
25
26    private IMethods mockOne;
27    private IMethods mockTwo;
28    private IMethods mockThree;
29    private InOrder inOrder;
30
31    @Before
32    public void setUp() {
33        mockOne = mock(IMethods.class);
34        mockTwo = mock(IMethods.class);
35        mockThree = mock(IMethods.class);
36
37        inOrder = inOrder(mockOne, mockTwo, mockThree);
38
39        mockOne.simpleMethod(1);
40        mockTwo.simpleMethod(2);
41        mockTwo.simpleMethod(2);
42        mockThree.simpleMethod(3);
43        mockTwo.simpleMethod(2);
44        mockOne.simpleMethod(4);
45    }
46
47    @Test
48    public void shouldVerifyInOrderAllInvocations() {
49        inOrder.verify(mockOne).simpleMethod(1);
50        inOrder.verify(mockTwo, times(2)).simpleMethod(2);
51        inOrder.verify(mockThree).simpleMethod(3);
52        inOrder.verify(mockTwo).simpleMethod(2);
53        inOrder.verify(mockOne).simpleMethod(4);
54        verifyNoMoreInteractions(mockOne, mockTwo, mockThree);
55    }
56
57    @Test
58    public void shouldVerifyInOrderAndBeRelaxed() {
59        inOrder.verify(mockTwo, times(2)).simpleMethod(2);
60        inOrder.verify(mockThree).simpleMethod(3);
61
62        verifyNoMoreInteractions(mockThree);
63    }
64
65    @Test
66    public void shouldAllowFirstChunkBeforeLastInvocation() {
67        inOrder.verify(mockTwo, times(2)).simpleMethod(2);
68        inOrder.verify(mockOne).simpleMethod(4);
69
70        try {
71            verifyNoMoreInteractions(mockTwo);
72            fail();
73        } catch (NoInteractionsWanted e) {}
74    }
75
76    @Test
77    public void shouldAllowAllChunksBeforeLastInvocation() {
78        inOrder.verify(mockTwo, times(3)).simpleMethod(2);
79        inOrder.verify(mockOne).simpleMethod(4);
80
81        verifyNoMoreInteractions(mockTwo);
82    }
83
84    @Test
85    public void shouldVerifyDetectFirstChunkOfInvocationThatExistInManyChunks() {
86        inOrder.verify(mockTwo, times(2)).simpleMethod(2);
87        inOrder.verify(mockThree).simpleMethod(3);
88        try {
89            verifyNoMoreInteractions(mockTwo);
90            fail();
91        } catch(NoInteractionsWanted e) {}
92    }
93
94    @Test
95    public void shouldVerifyDetectAllChunksOfInvocationThatExistInManyChunks() {
96        inOrder.verify(mockTwo, times(3)).simpleMethod(2);
97        inOrder.verify(mockOne).simpleMethod(4);
98        verifyNoMoreInteractions(mockTwo);
99    }
100
101    @Test
102    public void shouldVerifyInteractionsFromAllChunksWhenAtLeastOnceMode() {
103        inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
104        verifyNoMoreInteractions(mockTwo);
105        try {
106            inOrder.verify(mockThree).simpleMethod(3);
107            fail();
108        } catch (VerificationInOrderFailure e) {}
109    }
110
111    @Test
112    public void shouldVerifyInteractionsFromFirstChunk() {
113        inOrder.verify(mockTwo, times(2)).simpleMethod(2);
114        try {
115            verifyNoMoreInteractions(mockTwo);
116            fail();
117        } catch (NoInteractionsWanted e) {}
118    }
119
120    @Test(expected=VerificationInOrderFailure.class)
121    public void shouldFailVerificationOfNonFirstChunk() {
122        inOrder.verify(mockTwo, times(1)).simpleMethod(2);
123    }
124
125    @Test
126    public void shouldPassOnCombinationOfTimesAndAtLeastOnce() {
127        mockTwo.simpleMethod(2);
128
129        inOrder.verify(mockTwo, times(2)).simpleMethod(2);
130        inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
131        verifyNoMoreInteractions(mockTwo);
132    }
133
134    @Test
135    public void shouldPassOnEdgyCombinationOfTimesAndAtLeastOnce() {
136        mockTwo.simpleMethod(2);
137        mockThree.simpleMethod(3);
138
139        inOrder.verify(mockThree).simpleMethod(3);
140        inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
141        inOrder.verify(mockThree).simpleMethod(3);
142
143        verifyNoMoreInteractions(mockThree);
144    }
145
146    @Test
147    public void shouldVerifyInOrderMockTwoAndThree() {
148        inOrder.verify(mockTwo, times(2)).simpleMethod(2);
149        inOrder.verify(mockThree).simpleMethod(3);
150        inOrder.verify(mockTwo).simpleMethod(2);
151        verifyNoMoreInteractions(mockTwo, mockThree);
152    }
153
154    @Test
155    public void shouldVerifyInOrderMockOneAndThree() {
156        inOrder.verify(mockOne).simpleMethod(1);
157        inOrder.verify(mockThree).simpleMethod(3);
158        inOrder.verify(mockOne).simpleMethod(4);
159        verifyNoMoreInteractions(mockOne, mockThree);
160    }
161
162    @Test
163    public void shouldVerifyInOrderOnlyTwoInvocations() {
164        inOrder.verify(mockTwo, times(2)).simpleMethod(2);
165        inOrder.verify(mockOne).simpleMethod(4);
166    }
167
168    @Test
169    public void shouldVerifyInOrderOnlyMockTwo() {
170        inOrder.verify(mockTwo, times(2)).simpleMethod(2);
171        inOrder.verify(mockTwo).simpleMethod(2);
172        verifyNoMoreInteractions(mockTwo);
173    }
174
175    @Test
176    public void shouldVerifyMockTwoCalledTwice() {
177        inOrder.verify(mockTwo, times(2)).simpleMethod(2);
178    }
179
180    @Test
181    public void shouldVerifyMockTwoCalledAtLeastOnce() {
182        inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
183    }
184
185    @Test(expected=WantedButNotInvoked.class)
186    public void shouldFailOnWrongMethodCalledOnMockTwo() {
187        inOrder.verify(mockTwo, atLeastOnce()).differentMethod();
188    }
189
190    @Test
191    public void shouldAllowTimesZeroButOnlyInOrder() {
192        inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
193        inOrder.verify(mockOne, times(0)).simpleMethod(1);
194
195        try {
196            verify(mockOne, times(0)).simpleMethod(1);
197            fail();
198        } catch (NeverWantedButInvoked e) {}
199    }
200
201    @Test
202    public void shouldFailTimesZeroInOrder() {
203        inOrder.verify(mockTwo, times(2)).simpleMethod(2);
204        try {
205            inOrder.verify(mockThree, times(0)).simpleMethod(3);
206            fail();
207        } catch (VerificationInOrderFailure e) {}
208    }
209
210    @Test(expected=VerificationInOrderFailure.class)
211    public void shouldFailWhenMockTwoWantedZeroTimes() {
212        inOrder.verify(mockTwo, times(0)).simpleMethod(2);
213    }
214
215    @Test
216    public void shouldVerifyLastInvocation() {
217        inOrder.verify(mockOne).simpleMethod(4);
218    }
219
220    @Test
221    public void shouldVerifySecondAndLastInvocation() {
222        inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
223        inOrder.verify(mockOne).simpleMethod(4);
224    }
225
226    @Test
227    public void shouldVerifySecondAndLastInvocationWhenAtLeastOnceUsed() {
228        inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
229        inOrder.verify(mockOne).simpleMethod(4);
230    }
231
232    @Test
233    public void shouldFailOnLastTwoInvocationsInWrongOrder() {
234        inOrder.verify(mockOne).simpleMethod(4);
235        try {
236            inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
237            fail();
238        } catch (VerificationInOrderFailure e) {}
239    }
240
241    @Test
242    public void shouldFailOnLastAndFirstInWrongOrder() {
243        inOrder.verify(mockOne).simpleMethod(4);
244        try {
245            inOrder.verify(mockOne).simpleMethod(1);
246            fail();
247        } catch (VerificationInOrderFailure e) {}
248    }
249
250    @Test
251    public void shouldFailOnWrongMethodAfterLastInvocation() {
252        inOrder.verify(mockOne).simpleMethod(4);
253        try {
254            inOrder.verify(mockOne).simpleMethod(999);
255            fail();
256        } catch (VerificationInOrderFailure e) {}
257    }
258}
259