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.mockito.Mockito.after; 8 import static org.mockito.Mockito.inOrder; 9 import static org.mockito.Mockito.timeout; 10 import static org.mockito.junit.MockitoJUnit.rule; 11 12 import org.assertj.core.api.Assertions; 13 import org.assertj.core.api.ThrowableAssert; 14 import org.junit.After; 15 import org.junit.Before; 16 import org.junit.Rule; 17 import org.junit.Test; 18 import org.mockito.InOrder; 19 import org.mockito.Mock; 20 import org.mockito.exceptions.base.MockitoException; 21 import org.mockito.exceptions.verification.VerificationInOrderFailure; 22 import org.mockito.junit.MockitoRule; 23 import org.mockitousage.IMethods; 24 import org.mockitoutil.async.AsyncTesting; 25 26 public class VerificationInOrderWithTimeoutTest { 27 28 @Rule public MockitoRule mockito = rule(); 29 30 @Mock private IMethods mock1; 31 @Mock private IMethods mock2; 32 33 private AsyncTesting async; 34 35 @Before setUp()36 public void setUp() { 37 async = new AsyncTesting(); 38 } 39 40 @After tearDown()41 public void tearDown() { 42 async.cleanUp(); 43 } 44 45 @Test should_not_allow_in_order_with_after()46 public void should_not_allow_in_order_with_after() { 47 // expect 48 Assertions.assertThatThrownBy( 49 new ThrowableAssert.ThrowingCallable() { 50 public void call() { 51 inOrder(mock1).verify(mock1, after(100)).oneArg('a'); 52 } 53 }) 54 .isInstanceOf(MockitoException.class) 55 .hasMessageContaining("not implemented to work with InOrder"); 56 // TODO specific exception 57 } 58 59 @Test should_verify_in_order_with_timeout()60 public void should_verify_in_order_with_timeout() { 61 // when 62 async.runAfter(20, callMock(mock1, 'a')); 63 async.runAfter(50, callMock(mock1, 'c')); 64 async.runAfter(200, callMock(mock2, 'b')); 65 66 // then 67 InOrder inOrder = inOrder(mock1, mock2); 68 inOrder.verify(mock1, timeout(100)).oneArg('a'); 69 inOrder.verify(mock2, timeout(500)).oneArg('b'); 70 } 71 72 @Test should_verify_in_order_with_timeout_and_fail()73 public void should_verify_in_order_with_timeout_and_fail() { 74 // when 75 async.runAfter(20, callMock(mock1, 'a')); 76 async.runAfter(100, callMock(mock2, 'b')); 77 78 // then 79 final InOrder inOrder = inOrder(mock1, mock2); 80 inOrder.verify(mock2, timeout(300)).oneArg('b'); 81 Assertions.assertThatThrownBy( 82 new ThrowableAssert.ThrowingCallable() { 83 public void call() { 84 inOrder.verify(mock1, timeout(300)).oneArg('a'); 85 } 86 }) 87 .isInstanceOf(VerificationInOrderFailure.class) 88 .hasMessageContaining("Wanted but not invoked:\nmock1.oneArg('a');") 89 .hasMessageContaining( 90 "Wanted anywhere AFTER following interaction:\nmock2.oneArg('b');"); 91 } 92 93 @Test should_verify_in_order_with_times_x()94 public void should_verify_in_order_with_times_x() { 95 // when 96 async.runAfter(20, callMock(mock1, 'a')); 97 async.runAfter(50, callMock(mock1, 'a')); 98 async.runAfter(200, callMock(mock2, 'b')); 99 async.runAfter(250, callMock(mock2, 'b')); 100 101 // then 102 InOrder inOrder = inOrder(mock1, mock2); 103 inOrder.verify(mock1, timeout(100).times(2)).oneArg('a'); 104 inOrder.verify(mock2, timeout(500).times(2)).oneArg('b'); 105 } 106 107 @Test should_verify_in_order_with_times_x_and_fail()108 public void should_verify_in_order_with_times_x_and_fail() { 109 // when 110 async.runAfter(20, callMock(mock1, 'a')); 111 async.runAfter(50, callMock(mock1, 'a')); 112 async.runAfter(200, callMock(mock2, 'b')); 113 async.runAfter(250, callMock(mock2, 'b')); 114 115 // then 116 final InOrder inOrder = inOrder(mock1, mock2); 117 inOrder.verify(mock2, timeout(500).times(2)).oneArg('b'); 118 119 Assertions.assertThatThrownBy( 120 new ThrowableAssert.ThrowingCallable() { 121 public void call() { 122 inOrder.verify(mock1, timeout(100).times(2)).oneArg('a'); 123 } 124 }) 125 .isInstanceOf(VerificationInOrderFailure.class) 126 .hasMessageContaining("Wanted but not invoked:\nmock1.oneArg('a');") 127 .hasMessageContaining( 128 "Wanted anywhere AFTER following interaction:\nmock2.oneArg('b');"); 129 } 130 131 @Test should_not_allow_in_order_with_only()132 public void should_not_allow_in_order_with_only() { 133 Assertions.assertThatThrownBy( 134 new ThrowableAssert.ThrowingCallable() { 135 @Override 136 public void call() throws Throwable { 137 inOrder(mock1).verify(mock1, timeout(200).only()).oneArg('a'); 138 } 139 }) 140 .isInstanceOf(MockitoException.class) 141 .hasMessageContaining("not implemented to work with InOrder"); 142 // TODO specific exception 143 } 144 145 @Test should_verify_in_order_with_at_least_once()146 public void should_verify_in_order_with_at_least_once() { 147 // when 148 async.runAfter(20, callMock(mock1, 'a')); 149 async.runAfter(50, callMock(mock1, 'a')); 150 async.runAfter(100, callMock(mock2, 'b')); 151 async.runAfter(120, callMock(mock2, 'b')); 152 153 // then 154 InOrder inOrder = inOrder(mock1, mock2); 155 inOrder.verify(mock1, timeout(200).atLeastOnce()).oneArg('a'); 156 inOrder.verify(mock2, timeout(500).atLeastOnce()).oneArg('b'); 157 } 158 159 @Test should_verify_in_order_with_at_least_once_and_fail()160 public void should_verify_in_order_with_at_least_once_and_fail() { 161 // when 162 async.runAfter(20, callMock(mock1, 'a')); 163 async.runAfter(50, callMock(mock1, 'a')); 164 async.runAfter(100, callMock(mock2, 'b')); 165 async.runAfter(120, callMock(mock2, 'b')); 166 167 // then 168 final InOrder inOrder = inOrder(mock1, mock2); 169 inOrder.verify(mock2, timeout(300).atLeastOnce()).oneArg('b'); 170 Assertions.assertThatThrownBy( 171 new ThrowableAssert.ThrowingCallable() { 172 public void call() { 173 inOrder.verify(mock1, timeout(500).atLeastOnce()).oneArg('a'); 174 } 175 }) 176 .isInstanceOf(VerificationInOrderFailure.class) 177 .hasMessageContaining("Wanted but not invoked:\nmock1.oneArg('a');") 178 .hasMessageContaining( 179 "Wanted anywhere AFTER following interaction:\nmock2.oneArg('b');"); 180 } 181 182 @Test should_verify_in_order_with_at_least_x()183 public void should_verify_in_order_with_at_least_x() { 184 // when 185 async.runAfter(20, callMock(mock1, 'a')); 186 async.runAfter(50, callMock(mock1, 'a')); 187 async.runAfter(100, callMock(mock2, 'b')); 188 async.runAfter(120, callMock(mock2, 'b')); 189 190 // then 191 InOrder inOrder = inOrder(mock1, mock2); 192 inOrder.verify(mock1, timeout(200).atLeast(2)).oneArg('a'); 193 inOrder.verify(mock2, timeout(500).atLeast(2)).oneArg('b'); 194 } 195 196 @Test should_verify_in_order_with_at_least_x_and_fail()197 public void should_verify_in_order_with_at_least_x_and_fail() { 198 // when 199 async.runAfter(20, callMock(mock1, 'a')); 200 async.runAfter(50, callMock(mock1, 'a')); 201 async.runAfter(100, callMock(mock2, 'b')); 202 async.runAfter(120, callMock(mock2, 'b')); 203 204 // then 205 final InOrder inOrder = inOrder(mock1, mock2); 206 inOrder.verify(mock2, timeout(300).atLeast(2)).oneArg('b'); 207 Assertions.assertThatThrownBy( 208 new ThrowableAssert.ThrowingCallable() { 209 public void call() { 210 inOrder.verify(mock1, timeout(500).atLeast(2)).oneArg('a'); 211 } 212 }) 213 .isInstanceOf(AssertionError.class) 214 .hasMessageContaining("Verification in order failure"); 215 } 216 callMock(final IMethods mock, final char c)217 private Runnable callMock(final IMethods mock, final char c) { 218 return new Runnable() { 219 @Override 220 public void run() { 221 mock.oneArg(c); 222 } 223 }; 224 } 225 } 226