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