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.Ignore; 12 import org.junit.Rule; 13 import org.junit.Test; 14 import org.mockito.Mock; 15 import org.mockito.exceptions.verification.MoreThanAllowedActualInvocations; 16 import org.mockito.exceptions.verification.NoInteractionsWanted; 17 import org.mockito.exceptions.verification.TooManyActualInvocations; 18 import org.mockito.junit.MockitoRule; 19 import org.mockitousage.IMethods; 20 import org.mockitoutil.Stopwatch; 21 import org.mockitoutil.async.AsyncTesting; 22 23 import static java.util.concurrent.TimeUnit.MILLISECONDS; 24 import static org.assertj.core.api.Assertions.assertThatThrownBy; 25 import static org.mockito.Mockito.after; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.junit.MockitoJUnit.rule; 28 import static org.mockitoutil.Stopwatch.createNotStarted; 29 30 public class VerificationWithAfterTest { 31 32 @Rule public MockitoRule mockito = rule(); 33 34 @Mock private IMethods mock; 35 36 private Runnable callMock = new Runnable() { 37 public void run() { 38 mock.oneArg('1'); 39 } 40 }; 41 42 private AsyncTesting async = new AsyncTesting(); 43 private Stopwatch watch = createNotStarted(); 44 tearDown()45 @After public void tearDown() { 46 async.cleanUp(); 47 } 48 49 @Test should_verify_with_after()50 public void should_verify_with_after() { 51 // given 52 async.runAfter(10, callMock); 53 async.runAfter(1000, callMock); 54 55 // then 56 verify(mock, after(300)).oneArg('1'); 57 } 58 59 @Test should_verify_with_after_and_fail()60 public void should_verify_with_after_and_fail() { 61 // given 62 async.runAfter(10, callMock); 63 async.runAfter(40, callMock); 64 65 // then 66 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 67 @Override 68 public void call() { 69 verify(mock, after(300)).oneArg('1'); 70 } 71 }).isInstanceOf(TooManyActualInvocations.class); 72 } 73 74 @Test should_verify_with_time_x()75 public void should_verify_with_time_x() { 76 // given 77 async.runAfter(10, callMock); 78 async.runAfter(50, callMock); 79 async.runAfter(600, callMock); 80 81 // then 82 verify(mock, after(300).times(2)).oneArg('1'); 83 } 84 85 @Test should_verify_with_time_x_and_fail()86 public void should_verify_with_time_x_and_fail() { 87 // given 88 async.runAfter(10, callMock); 89 async.runAfter(40, callMock); 90 async.runAfter(80, callMock); 91 92 // then 93 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 94 @Override 95 public void call() { 96 verify(mock, after(300).times(2)).oneArg('1'); 97 } 98 }).isInstanceOf(TooManyActualInvocations.class); 99 } 100 101 @Test should_verify_with_at_least()102 public void should_verify_with_at_least() { 103 // given 104 async.runAfter(10, callMock); 105 async.runAfter(50, callMock); 106 107 // then 108 verify(mock, after(300).atLeastOnce()).oneArg('1'); 109 } 110 111 @Test should_verify_with_at_least_and_fail()112 public void should_verify_with_at_least_and_fail() { 113 // given 114 async.runAfter(10, callMock); 115 async.runAfter(50, callMock); 116 async.runAfter(600, callMock); 117 118 // then 119 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 120 @Override 121 public void call() { 122 verify(mock, after(300).atLeast(3)).oneArg('1'); 123 } 124 }).isInstanceOf(AssertionError.class).hasMessageContaining("Wanted *at least* 3 times"); //TODO specific exception 125 } 126 127 @Test should_verify_with_at_most()128 public void should_verify_with_at_most() { 129 // given 130 async.runAfter(10, callMock); 131 async.runAfter(50, callMock); 132 async.runAfter(600, callMock); 133 134 // then 135 verify(mock, after(300).atMost(2)).oneArg('1'); 136 } 137 138 @Test should_verify_with_at_most_and_fail()139 public void should_verify_with_at_most_and_fail() { 140 // given 141 async.runAfter(10, callMock); 142 async.runAfter(50, callMock); 143 async.runAfter(600, callMock); 144 145 // then 146 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 147 @Override 148 public void call() { 149 verify(mock, after(300).atMost(1)).oneArg('1'); 150 } 151 }).isInstanceOf(AssertionError.class).hasMessageContaining("Wanted at most 1 time but was 2"); //TODO specific exception 152 } 153 154 @Test should_verify_with_never()155 public void should_verify_with_never() { 156 // given 157 async.runAfter(500, callMock); 158 159 // then 160 verify(mock, after(50).never()).oneArg('1'); 161 } 162 163 @Test should_verify_with_never_and_fail()164 public void should_verify_with_never_and_fail() { 165 // given 166 async.runAfter(10, callMock); 167 168 // then 169 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 170 @Override 171 public void call() { 172 verify(mock, after(300).never()).oneArg('1'); 173 } 174 }).isInstanceOf(MoreThanAllowedActualInvocations.class).hasMessageContaining("Wanted at most 0 times but was 1"); 175 } 176 177 @Test should_verify_with_only()178 public void should_verify_with_only() { 179 // given 180 async.runAfter(10, callMock); 181 async.runAfter(600, callMock); 182 183 // then 184 verify(mock, after(300).only()).oneArg('1'); 185 } 186 187 @Test should_verify_with_only_and_fail()188 public void should_verify_with_only_and_fail() { 189 // given 190 async.runAfter(10, callMock); 191 async.runAfter(50, callMock); 192 193 // then 194 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 195 @Override 196 public void call() { 197 verify(mock, after(300).only()).oneArg('1'); 198 } 199 }).isInstanceOf(AssertionError.class).hasMessageContaining("No interactions wanted here"); //TODO specific exception 200 } 201 202 @Test should_fail_early_when_at_most_is_used()203 public void should_fail_early_when_at_most_is_used() { 204 watch.start(); 205 206 // when 207 async.runAfter(50, callMock); 208 async.runAfter(100, callMock); 209 210 // then 211 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 212 public void call() { 213 verify(mock, after(10000).atMost(1)).oneArg('1'); 214 } 215 }).isInstanceOf(MoreThanAllowedActualInvocations.class); 216 217 // using generous number to avoid timing issues 218 watch.assertElapsedTimeIsLessThan(2000, MILLISECONDS); 219 } 220 221 @Test should_fail_early_when_never_is_used()222 public void should_fail_early_when_never_is_used() { 223 watch.start(); 224 225 // when 226 async.runAfter(50, callMock); 227 228 // then 229 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 230 public void call() { 231 verify(mock, after(10000).never()).oneArg('1'); 232 } 233 }).isInstanceOf(MoreThanAllowedActualInvocations.class); 234 235 // using generous number to avoid timing issues 236 watch.assertElapsedTimeIsLessThan(2000, MILLISECONDS); 237 } 238 239 @Test 240 @Ignore //TODO nice to have should_fail_early_when_only_is_used()241 public void should_fail_early_when_only_is_used() { 242 watch.start(); 243 244 // when 245 async.runAfter(50, callMock); 246 async.runAfter(100, callMock); 247 248 // then 249 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 250 public void call() { 251 verify(mock, after(10000).only()).oneArg('1'); 252 } 253 }).isInstanceOf(NoInteractionsWanted.class); 254 255 // using generous number to avoid timing issues 256 watch.assertElapsedTimeIsLessThan(2000, MILLISECONDS); 257 } 258 259 @Test 260 @Ignore //TODO nice to have should_fail_early_when_time_x_is_used()261 public void should_fail_early_when_time_x_is_used() { 262 watch.start(); 263 264 // when 265 async.runAfter(50, callMock); 266 async.runAfter(100, callMock); 267 268 // then 269 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 270 public void call() { 271 verify(mock, after(10000).times(1)).oneArg('1'); 272 } 273 }).isInstanceOf(NoInteractionsWanted.class); 274 275 // using generous number to avoid timing issues 276 watch.assertElapsedTimeIsLessThan(2000, MILLISECONDS); 277 } 278 } 279