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.verification.NeverWantedButInvoked; 12 import org.mockito.exceptions.verification.NoInteractionsWanted; 13 import org.mockito.exceptions.verification.VerificationInOrderFailure; 14 import org.mockito.exceptions.verification.WantedButNotInvoked; 15 import org.mockitousage.IMethods; 16 import org.mockitoutil.TestBase; 17 18 import static org.junit.Assert.fail; 19 import static org.mockito.Mockito.*; 20 21 /** 22 * ignored since 'relaxed' in order verification is not implemented (too complex to bother, maybe later). 23 */ 24 public class RelaxedVerificationInOrderTest extends TestBase { 25 26 private IMethods mockOne; 27 private IMethods mockTwo; 28 private IMethods mockThree; 29 private InOrder inOrder; 30 31 @Before setUp()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 shouldVerifyInOrderAllInvocations()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 shouldVerifyInOrderAndBeRelaxed()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 shouldAllowFirstChunkBeforeLastInvocation()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 shouldAllowAllChunksBeforeLastInvocation()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 shouldVerifyDetectFirstChunkOfInvocationThatExistInManyChunks()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 shouldVerifyDetectAllChunksOfInvocationThatExistInManyChunks()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 shouldVerifyInteractionsFromAllChunksWhenAtLeastOnceMode()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 shouldVerifyInteractionsFromFirstChunk()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) shouldFailVerificationOfNonFirstChunk()121 public void shouldFailVerificationOfNonFirstChunk() { 122 inOrder.verify(mockTwo, times(1)).simpleMethod(2); 123 } 124 125 @Test shouldPassOnCombinationOfTimesAndAtLeastOnce()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 shouldPassOnEdgyCombinationOfTimesAndAtLeastOnce()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 shouldVerifyInOrderMockTwoAndThree()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 shouldVerifyInOrderMockOneAndThree()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 shouldVerifyInOrderOnlyTwoInvocations()163 public void shouldVerifyInOrderOnlyTwoInvocations() { 164 inOrder.verify(mockTwo, times(2)).simpleMethod(2); 165 inOrder.verify(mockOne).simpleMethod(4); 166 } 167 168 @Test shouldVerifyInOrderOnlyMockTwo()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 shouldVerifyMockTwoCalledTwice()176 public void shouldVerifyMockTwoCalledTwice() { 177 inOrder.verify(mockTwo, times(2)).simpleMethod(2); 178 } 179 180 @Test shouldVerifyMockTwoCalledAtLeastOnce()181 public void shouldVerifyMockTwoCalledAtLeastOnce() { 182 inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2); 183 } 184 185 @Test(expected=WantedButNotInvoked.class) shouldFailOnWrongMethodCalledOnMockTwo()186 public void shouldFailOnWrongMethodCalledOnMockTwo() { 187 inOrder.verify(mockTwo, atLeastOnce()).differentMethod(); 188 } 189 190 @Test shouldAllowTimesZeroButOnlyInOrder()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 shouldFailTimesZeroInOrder()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) shouldFailWhenMockTwoWantedZeroTimes()211 public void shouldFailWhenMockTwoWantedZeroTimes() { 212 inOrder.verify(mockTwo, times(0)).simpleMethod(2); 213 } 214 215 @Test shouldVerifyLastInvocation()216 public void shouldVerifyLastInvocation() { 217 inOrder.verify(mockOne).simpleMethod(4); 218 } 219 220 @Test shouldVerifySecondAndLastInvocation()221 public void shouldVerifySecondAndLastInvocation() { 222 inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2); 223 inOrder.verify(mockOne).simpleMethod(4); 224 } 225 226 @Test shouldVerifySecondAndLastInvocationWhenAtLeastOnceUsed()227 public void shouldVerifySecondAndLastInvocationWhenAtLeastOnceUsed() { 228 inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2); 229 inOrder.verify(mockOne).simpleMethod(4); 230 } 231 232 @Test shouldFailOnLastTwoInvocationsInWrongOrder()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 shouldFailOnLastAndFirstInWrongOrder()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 shouldFailOnWrongMethodAfterLastInvocation()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