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.junit.Assert.fail; 8 import static org.mockito.Mockito.*; 9 10 import org.junit.Before; 11 import org.junit.Test; 12 import org.mockito.InOrder; 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 public class SelectedMocksInOrderVerificationTest extends TestBase { 19 20 private IMethods mockOne; 21 private IMethods mockTwo; 22 private IMethods mockThree; 23 24 @Before setUp()25 public void setUp() { 26 mockOne = mock(IMethods.class); 27 mockTwo = mock(IMethods.class); 28 mockThree = mock(IMethods.class); 29 30 mockOne.simpleMethod(1); 31 mockTwo.simpleMethod(2); 32 mockTwo.simpleMethod(2); 33 mockThree.simpleMethod(3); 34 mockTwo.simpleMethod(2); 35 mockOne.simpleMethod(4); 36 } 37 38 @Test shouldVerifyAllInvocationsInOrder()39 public void shouldVerifyAllInvocationsInOrder() { 40 InOrder inOrder = inOrder(mockOne, mockTwo, mockThree); 41 inOrder.verify(mockOne).simpleMethod(1); 42 inOrder.verify(mockTwo, times(2)).simpleMethod(2); 43 inOrder.verify(mockThree).simpleMethod(3); 44 inOrder.verify(mockTwo).simpleMethod(2); 45 inOrder.verify(mockOne).simpleMethod(4); 46 verifyNoMoreInteractions(mockOne, mockTwo, mockThree); 47 } 48 49 @Test shouldVerifyInOrderMockTwoAndThree()50 public void shouldVerifyInOrderMockTwoAndThree() { 51 InOrder inOrder = inOrder(mockTwo, mockThree); 52 53 inOrder.verify(mockTwo, times(2)).simpleMethod(2); 54 inOrder.verify(mockThree).simpleMethod(3); 55 inOrder.verify(mockTwo).simpleMethod(2); 56 verifyNoMoreInteractions(mockTwo, mockThree); 57 } 58 59 @Test shouldVerifyInOrderMockOneAndThree()60 public void shouldVerifyInOrderMockOneAndThree() { 61 InOrder inOrder = inOrder(mockOne, mockThree); 62 63 inOrder.verify(mockOne).simpleMethod(1); 64 inOrder.verify(mockThree).simpleMethod(3); 65 inOrder.verify(mockOne).simpleMethod(4); 66 verifyNoMoreInteractions(mockOne, mockThree); 67 } 68 69 @Test shouldVerifyMockOneInOrder()70 public void shouldVerifyMockOneInOrder() { 71 InOrder inOrder = inOrder(mockOne); 72 73 inOrder.verify(mockOne).simpleMethod(1); 74 inOrder.verify(mockOne).simpleMethod(4); 75 76 verifyNoMoreInteractions(mockOne); 77 } 78 79 @Test shouldFailVerificationForMockOne()80 public void shouldFailVerificationForMockOne() { 81 InOrder inOrder = inOrder(mockOne); 82 83 inOrder.verify(mockOne).simpleMethod(1); 84 try { 85 inOrder.verify(mockOne).differentMethod(); 86 fail(); 87 } catch (VerificationInOrderFailure e) { 88 } 89 } 90 91 @Test shouldFailVerificationForMockOneBecauseOfWrongOrder()92 public void shouldFailVerificationForMockOneBecauseOfWrongOrder() { 93 InOrder inOrder = inOrder(mockOne); 94 95 inOrder.verify(mockOne).simpleMethod(4); 96 try { 97 inOrder.verify(mockOne).simpleMethod(1); 98 fail(); 99 } catch (VerificationInOrderFailure e) { 100 } 101 } 102 103 @Test shouldVerifyMockTwoWhenThreeTimesUsed()104 public void shouldVerifyMockTwoWhenThreeTimesUsed() { 105 InOrder inOrder = inOrder(mockTwo); 106 107 inOrder.verify(mockTwo, times(3)).simpleMethod(2); 108 109 verifyNoMoreInteractions(mockTwo); 110 } 111 112 @Test shouldVerifyMockTwo()113 public void shouldVerifyMockTwo() { 114 InOrder inOrder = inOrder(mockTwo); 115 116 inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2); 117 118 verifyNoMoreInteractions(mockTwo); 119 } 120 121 @Test shouldFailVerificationForMockTwo()122 public void shouldFailVerificationForMockTwo() { 123 InOrder inOrder = inOrder(mockTwo); 124 125 try { 126 inOrder.verify(mockTwo).simpleMethod(2); 127 fail(); 128 } catch (VerificationInOrderFailure e) { 129 } 130 } 131 132 @Test shouldThrowNoMoreInvocationsForMockTwo()133 public void shouldThrowNoMoreInvocationsForMockTwo() { 134 InOrder inOrder = inOrder(mockTwo); 135 136 try { 137 inOrder.verify(mockTwo, times(2)).simpleMethod(2); 138 fail(); 139 } catch (VerificationInOrderFailure e) { 140 } 141 } 142 143 @Test shouldThrowTooFewInvocationsForMockTwo()144 public void shouldThrowTooFewInvocationsForMockTwo() { 145 InOrder inOrder = inOrder(mockTwo); 146 147 try { 148 inOrder.verify(mockTwo, times(4)).simpleMethod(2); 149 fail(); 150 } catch (VerificationInOrderFailure e) { 151 } 152 } 153 154 @Test shouldThrowTooManyInvocationsForMockTwo()155 public void shouldThrowTooManyInvocationsForMockTwo() { 156 InOrder inOrder = inOrder(mockTwo); 157 158 try { 159 inOrder.verify(mockTwo, times(2)).simpleMethod(2); 160 fail(); 161 } catch (VerificationInOrderFailure e) { 162 } 163 } 164 165 @Test shouldAllowThreeTimesOnMockTwo()166 public void shouldAllowThreeTimesOnMockTwo() { 167 InOrder inOrder = inOrder(mockTwo); 168 169 inOrder.verify(mockTwo, times(3)).simpleMethod(2); 170 verifyNoMoreInteractions(mockTwo); 171 } 172 173 @Test shouldVerifyMockTwoCompletely()174 public void shouldVerifyMockTwoCompletely() { 175 InOrder inOrder = inOrder(mockTwo, mockThree); 176 177 inOrder.verify(mockTwo, times(2)).simpleMethod(2); 178 inOrder.verify(mockThree).simpleMethod(3); 179 inOrder.verify(mockTwo).simpleMethod(2); 180 verifyNoMoreInteractions(mockTwo, mockThree); 181 } 182 183 @Test shouldAllowTwoTimesOnMockTwo()184 public void shouldAllowTwoTimesOnMockTwo() { 185 InOrder inOrder = inOrder(mockTwo, mockThree); 186 187 inOrder.verify(mockTwo, times(2)).simpleMethod(2); 188 try { 189 verifyNoMoreInteractions(mockTwo); 190 fail(); 191 } catch (NoInteractionsWanted e) { 192 } 193 } 194 } 195