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