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.mockitousage.IMethods; 15 import org.mockitoutil.TestBase; 16 17 import static junit.framework.TestCase.*; 18 import static org.mockito.Mockito.*; 19 20 public class VerificationInOrderMixedWithOrdiraryVerificationTest extends TestBase { 21 22 private IMethods mockOne; 23 private IMethods mockTwo; 24 private IMethods mockThree; 25 private InOrder inOrder; 26 27 @Before 28 public void setUp() { 29 mockOne = mock(IMethods.class); 30 mockTwo = mock(IMethods.class); 31 mockThree = mock(IMethods.class); 32 33 mockOne.simpleMethod(1); 34 mockOne.simpleMethod(1); 35 mockTwo.simpleMethod(2); 36 mockThree.simpleMethod(3); 37 mockThree.simpleMethod(4); 38 39 inOrder = inOrder(mockOne, mockThree); 40 } 41 42 @Test 43 public void shouldMixVerificationInOrderAndOrdinaryVerification() { 44 inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1); 45 inOrder.verify(mockThree).simpleMethod(3); 46 inOrder.verify(mockThree).simpleMethod(4); 47 verify(mockTwo).simpleMethod(2); 48 49 verifyNoMoreInteractions(mockOne, mockTwo, mockThree); 50 } 51 52 @Test 53 public void shouldAllowOrdinarilyVerifyingMockPassedToInOrderObject() { 54 inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1); 55 56 verify(mockThree).simpleMethod(3); 57 verify(mockThree).simpleMethod(4); 58 verify(mockTwo).simpleMethod(2); 59 60 verifyNoMoreInteractions(mockOne, mockTwo, mockThree); 61 } 62 63 @Test 64 public void shouldAllowRedundantVerifications() { 65 verify(mockOne, atLeastOnce()).simpleMethod(1); 66 verify(mockTwo).simpleMethod(2); 67 verify(mockThree).simpleMethod(3); 68 verify(mockThree).simpleMethod(4); 69 70 inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1); 71 inOrder.verify(mockThree).simpleMethod(3); 72 inOrder.verify(mockThree).simpleMethod(4); 73 74 verifyNoMoreInteractions(mockOne, mockTwo, mockThree); 75 } 76 77 @Test 78 public void shouldFailOnNoMoreInteractions() { 79 inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1); 80 inOrder.verify(mockThree).simpleMethod(3); 81 inOrder.verify(mockThree).simpleMethod(4); 82 83 try { 84 verifyNoMoreInteractions(mockOne, mockTwo, mockThree); 85 fail(); 86 } catch (NoInteractionsWanted e) {} 87 } 88 89 @Test 90 public void shouldFailOnNoMoreInteractionsOnMockVerifiedInOrder() { 91 inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1); 92 inOrder.verify(mockThree).simpleMethod(3); 93 verify(mockTwo).simpleMethod(2); 94 95 try { 96 verifyNoMoreInteractions(mockOne, mockTwo, mockThree); 97 fail(); 98 } catch (NoInteractionsWanted e) {} 99 } 100 101 @Test 102 public void shouldAllowOneMethodVerifiedInOrder() { 103 verify(mockTwo).simpleMethod(2); 104 verify(mockOne, atLeastOnce()).simpleMethod(1); 105 106 inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1); 107 } 108 109 @Test 110 public void shouldFailOnLastInvocationTooEarly() { 111 inOrder.verify(mockThree).simpleMethod(4); 112 113 verify(mockThree).simpleMethod(4); 114 verify(mockTwo).simpleMethod(2); 115 116 try { 117 inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1); 118 fail(); 119 } catch (VerificationInOrderFailure e) {} 120 } 121 122 @Test(expected=MockitoException.class) 123 public void shouldScreamWhenUnfamiliarMockPassedToInOrderObject() { 124 inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(1); 125 } 126 127 @Test 128 public void shouldUseEqualsToVerifyMethodArguments() { 129 mockOne = mock(IMethods.class); 130 131 String textOne = "test"; 132 String textTwo = new String(textOne); 133 134 assertEquals(textOne, textTwo); 135 assertNotSame(textOne, textTwo); 136 137 mockOne.simpleMethod(textOne); 138 mockOne.simpleMethod(textTwo); 139 140 verify(mockOne, times(2)).simpleMethod(textOne); 141 142 inOrder = inOrder(mockOne); 143 inOrder.verify(mockOne, times(2)).simpleMethod(textOne); 144 } 145 146 @Test 147 public void shouldUseEqualsToVerifyMethodVarargs() { 148 mockOne = mock(IMethods.class); 149 150 String textOne = "test"; 151 String textTwo = new String(textOne); 152 153 assertEquals(textOne, textTwo); 154 assertNotSame(textOne, textTwo); 155 156 mockOne.varargsObject(1, textOne, textOne); 157 mockOne.varargsObject(1, textTwo, textTwo); 158 159 verify(mockOne, times(2)).varargsObject(1, textOne, textOne); 160 161 inOrder = inOrder(mockOne); 162 inOrder.verify(mockOne, times(2)).varargsObject(1, textOne, textOne); 163 } 164 } 165