1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito; 6 7 import org.mockito.stubbing.Answer; 8 import org.mockito.stubbing.OngoingStubbing; 9 import org.mockito.stubbing.Stubber; 10 import org.mockito.verification.VerificationMode; 11 12 /** 13 * Behavior Driven Development style of writing tests uses <b>//given //when //then</b> comments as fundamental parts of your test methods. 14 * This is exactly how we write our tests and we warmly encourage you to do so! 15 * <p> 16 * Start learning about BDD here: <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">http://en.wikipedia.org/wiki/Behavior_Driven_Development</a> 17 * <p> 18 * The problem is that current stubbing api with canonical role of <b>when</b> word does not integrate nicely with <b>//given //when //then</b> comments. 19 * It's because stubbing belongs to <b>given</b> component of the test and not to the <b>when</b> component of the test. 20 * Hence {@link BDDMockito} class introduces an alias so that you stub method calls with {@link BDDMockito#given(Object)} method. 21 * Now it really nicely integrates with the <b>given</b> component of a BDD style test! 22 * <p> 23 * Here is how the test might look like: 24 * <pre class="code"><code class="java"> 25 * import static org.mockito.BDDMockito.*; 26 * 27 * Seller seller = mock(Seller.class); 28 * Shop shop = new Shop(seller); 29 * 30 * public void shouldBuyBread() throws Exception { 31 * //given 32 * given(seller.askForBread()).willReturn(new Bread()); 33 * 34 * //when 35 * Goods goods = shop.buyBread(); 36 * 37 * //then 38 * assertThat(goods, containBread()); 39 * } 40 * </code></pre> 41 * 42 * Stubbing voids with throwables: 43 * <pre class="code"><code class="java"> 44 * //given 45 * willThrow(new RuntimeException("boo")).given(mock).foo(); 46 * 47 * //when 48 * Result result = systemUnderTest.perform(); 49 * 50 * //then 51 * assertEquals(failure, result); 52 * </code></pre> 53 * <p> 54 * For BDD style mock verification take a look at {@link Then} in action: 55 * <pre class="code"><code class="java"> 56 * person.ride(bike); 57 * person.ride(bike); 58 * 59 * then(person).should(times(2)).ride(bike); 60 * then(person).shouldHaveNoMoreInteractions(); 61 * then(police).shouldHaveZeroInteractions(); 62 * </code></pre> 63 * <p> 64 * It is also possible to do BDD style {@link InOrder} verification: 65 * <pre class="code"><code class="java"> 66 * InOrder inOrder = inOrder(person); 67 * 68 * person.drive(car); 69 * person.ride(bike); 70 * person.ride(bike); 71 * 72 * then(person).should(inOrder).drive(car); 73 * then(person).should(inOrder, times(2)).ride(bike); 74 * </code></pre> 75 * <p> 76 * One of the purposes of BDDMockito is also to show how to tailor the mocking syntax to a different programming style. 77 * 78 * @since 1.8.0 79 */ 80 @SuppressWarnings("unchecked") 81 public class BDDMockito extends Mockito { 82 83 /** 84 * See original {@link OngoingStubbing} 85 * @since 1.8.0 86 */ 87 public interface BDDMyOngoingStubbing<T> { 88 89 /** 90 * See original {@link OngoingStubbing#thenAnswer(Answer)} 91 * @since 1.8.0 92 */ willAnswer(Answer<?> answer)93 BDDMyOngoingStubbing<T> willAnswer(Answer<?> answer); 94 95 /** 96 * See original {@link OngoingStubbing#then(Answer)} 97 * @since 1.9.0 98 */ will(Answer<?> answer)99 BDDMyOngoingStubbing<T> will(Answer<?> answer); 100 101 /** 102 * See original {@link OngoingStubbing#thenReturn(Object)} 103 * @since 1.8.0 104 */ willReturn(T value)105 BDDMyOngoingStubbing<T> willReturn(T value); 106 107 /** 108 * See original {@link OngoingStubbing#thenReturn(Object, Object[])} 109 * @since 1.8.0 110 */ 111 @SuppressWarnings({"unchecked", "varargs"}) willReturn(T value, T... values)112 BDDMyOngoingStubbing<T> willReturn(T value, T... values); 113 114 /** 115 * See original {@link OngoingStubbing#thenThrow(Throwable...)} 116 * @since 1.8.0 117 */ willThrow(Throwable... throwables)118 BDDMyOngoingStubbing<T> willThrow(Throwable... throwables); 119 120 /** 121 * See original {@link OngoingStubbing#thenThrow(Class)} 122 * @since 2.1.0 123 */ willThrow(Class<? extends Throwable> throwableType)124 BDDMyOngoingStubbing<T> willThrow(Class<? extends Throwable> throwableType); 125 126 /** 127 * See original {@link OngoingStubbing#thenThrow(Class, Class[])} 128 * @since 2.1.0 129 */ 130 // Additional method helps users of JDK7+ to hide heap pollution / unchecked generics array creation 131 @SuppressWarnings ({"unchecked", "varargs"}) willThrow(Class<? extends Throwable> throwableType, Class<? extends Throwable>... throwableTypes)132 BDDMyOngoingStubbing<T> willThrow(Class<? extends Throwable> throwableType, Class<? extends Throwable>... throwableTypes); 133 134 /** 135 * See original {@link OngoingStubbing#thenCallRealMethod()} 136 * @since 1.9.0 137 */ willCallRealMethod()138 BDDMyOngoingStubbing<T> willCallRealMethod(); 139 140 /** 141 * See original {@link OngoingStubbing#getMock()} 142 * @since 1.9.0 143 */ getMock()144 <M> M getMock(); 145 } 146 147 private static class BDDOngoingStubbingImpl<T> implements BDDMyOngoingStubbing<T> { 148 149 private final OngoingStubbing<T> mockitoOngoingStubbing; 150 BDDOngoingStubbingImpl(OngoingStubbing<T> ongoingStubbing)151 public BDDOngoingStubbingImpl(OngoingStubbing<T> ongoingStubbing) { 152 this.mockitoOngoingStubbing = ongoingStubbing; 153 } 154 willAnswer(Answer<?> answer)155 public BDDMyOngoingStubbing<T> willAnswer(Answer<?> answer) { 156 return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenAnswer(answer)); 157 } 158 will(Answer<?> answer)159 public BDDMyOngoingStubbing<T> will(Answer<?> answer) { 160 return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.then(answer)); 161 } 162 willReturn(T value)163 public BDDMyOngoingStubbing<T> willReturn(T value) { 164 return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenReturn(value)); 165 } 166 willReturn(T value, T... values)167 public BDDMyOngoingStubbing<T> willReturn(T value, T... values) { 168 return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenReturn(value, values)); 169 } 170 willThrow(Throwable... throwables)171 public BDDMyOngoingStubbing<T> willThrow(Throwable... throwables) { 172 return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenThrow(throwables)); 173 } 174 willThrow(Class<? extends Throwable> throwableType)175 public BDDMyOngoingStubbing<T> willThrow(Class<? extends Throwable> throwableType) { 176 return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenThrow(throwableType)); 177 } 178 willThrow(Class<? extends Throwable> throwableType, Class<? extends Throwable>... throwableTypes)179 public BDDMyOngoingStubbing<T> willThrow(Class<? extends Throwable> throwableType, Class<? extends Throwable>... throwableTypes) { 180 return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenThrow(throwableType, throwableTypes)); 181 } 182 willCallRealMethod()183 public BDDMyOngoingStubbing<T> willCallRealMethod() { 184 return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenCallRealMethod()); 185 } 186 getMock()187 public <M> M getMock() { 188 return (M) mockitoOngoingStubbing.getMock(); 189 } 190 } 191 192 /** 193 * see original {@link Mockito#when(Object)} 194 * @since 1.8.0 195 */ given(T methodCall)196 public static <T> BDDMyOngoingStubbing<T> given(T methodCall) { 197 return new BDDOngoingStubbingImpl<T>(Mockito.when(methodCall)); 198 } 199 200 /** 201 * Bdd style verification of mock behavior. 202 * 203 * <pre class="code"><code class="java"> 204 * person.ride(bike); 205 * person.ride(bike); 206 * 207 * then(person).should(times(2)).ride(bike); 208 * </code></pre> 209 * 210 * @see #verify(Object) 211 * @see #verify(Object, VerificationMode) 212 * @since 1.10.0 213 */ then(T mock)214 public static <T> Then<T> then(T mock) { 215 return new ThenImpl<T>(mock); 216 } 217 218 /** 219 * Provides fluent way of mock verification. 220 * 221 * @param <T> type of the mock 222 * 223 * @since 1.10.5 224 */ 225 public interface Then<T> { 226 227 /** 228 * @see #verify(Object) 229 * @since 1.10.5 230 */ should()231 T should(); 232 233 /** 234 * @see #verify(Object, VerificationMode) 235 * @since 1.10.5 236 */ should(VerificationMode mode)237 T should(VerificationMode mode); 238 239 /** 240 * @see InOrder#verify(Object) 241 * @since 2.1.0 242 */ should(InOrder inOrder)243 T should(InOrder inOrder); 244 245 /** 246 * @see InOrder#verify(Object, VerificationMode) 247 * @since 2.1.0 248 */ should(InOrder inOrder, VerificationMode mode)249 T should(InOrder inOrder, VerificationMode mode); 250 251 /** 252 * @see #verifyZeroInteractions(Object...) 253 * @since 2.1.0 254 */ shouldHaveZeroInteractions()255 void shouldHaveZeroInteractions(); 256 257 /** 258 * @see #verifyNoMoreInteractions(Object...) 259 * @since 2.1.0 260 */ shouldHaveNoMoreInteractions()261 void shouldHaveNoMoreInteractions(); 262 } 263 264 private static class ThenImpl<T> implements Then<T> { 265 266 private final T mock; 267 ThenImpl(T mock)268 ThenImpl(T mock) { 269 this.mock = mock; 270 } 271 272 /** 273 * @see #verify(Object) 274 * @since 1.10.5 275 */ should()276 public T should() { 277 return verify(mock); 278 } 279 280 /** 281 * @see #verify(Object, VerificationMode) 282 * @since 1.10.5 283 */ should(VerificationMode mode)284 public T should(VerificationMode mode) { 285 return verify(mock, mode); 286 } 287 288 /** 289 * @see InOrder#verify(Object) 290 * @since 2.1.0 291 */ should(InOrder inOrder)292 public T should(InOrder inOrder) { 293 return inOrder.verify(mock); 294 } 295 296 /** 297 * @see InOrder#verify(Object, VerificationMode) 298 * @since 2.1.0 299 */ should(InOrder inOrder, VerificationMode mode)300 public T should(InOrder inOrder, VerificationMode mode) { 301 return inOrder.verify(mock, mode); 302 } 303 304 /** 305 * @see #verifyZeroInteractions(Object...) 306 * @since 2.1.0 307 */ shouldHaveZeroInteractions()308 public void shouldHaveZeroInteractions() { 309 verifyZeroInteractions(mock); 310 } 311 312 /** 313 * @see #verifyNoMoreInteractions(Object...) 314 * @since 2.1.0 315 */ shouldHaveNoMoreInteractions()316 public void shouldHaveNoMoreInteractions() { 317 verifyNoMoreInteractions(mock); 318 } 319 } 320 321 /** 322 * See original {@link Stubber} 323 * @since 1.8.0 324 */ 325 public interface BDDStubber { 326 /** 327 * See original {@link Stubber#doAnswer(Answer)} 328 * @since 1.8.0 329 */ willAnswer(Answer<?> answer)330 BDDStubber willAnswer(Answer<?> answer); 331 332 /** 333 * See original {@link Stubber#doAnswer(Answer)} 334 * @since 1.8.0 335 */ will(Answer<?> answer)336 BDDStubber will(Answer<?> answer); 337 338 /** 339 * See original {@link Stubber#doNothing()}. 340 * 341 * This method will be removed in version 3.0.0 342 * 343 * @since 1.8.0 344 * @deprecated as of 2.1.0 please use {@link #willDoNothing()} instead 345 */ 346 @Deprecated willNothing()347 BDDStubber willNothing(); 348 349 /** 350 * See original {@link Stubber#doNothing()} 351 * @since 1.10.20 352 */ willDoNothing()353 BDDStubber willDoNothing(); 354 355 /** 356 * See original {@link Stubber#doReturn(Object)} 357 * @since 2.1.0 358 */ willReturn(Object toBeReturned)359 BDDStubber willReturn(Object toBeReturned); 360 361 /** 362 * See original {@link Stubber#doReturn(Object)} 363 * @since 2.1.0 364 */ 365 @SuppressWarnings({"unchecked", "varargs"}) willReturn(Object toBeReturned, Object... nextToBeReturned)366 BDDStubber willReturn(Object toBeReturned, Object... nextToBeReturned); 367 368 /** 369 * See original {@link Stubber#doThrow(Throwable...)} 370 * @since 1.8.0 371 */ willThrow(Throwable... toBeThrown)372 BDDStubber willThrow(Throwable... toBeThrown); 373 374 /** 375 * See original {@link Stubber#doThrow(Class)} 376 * @since 2.1.0 377 */ willThrow(Class<? extends Throwable> toBeThrown)378 BDDStubber willThrow(Class<? extends Throwable> toBeThrown); 379 380 /** 381 * See original {@link Stubber#doThrow(Class, Class[])} 382 * @since 2.1.0 383 */ 384 @SuppressWarnings ({"unchecked", "varargs"}) willThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown)385 BDDStubber willThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown); 386 387 /** 388 * See original {@link Stubber#doCallRealMethod()} 389 * @since 1.9.0 390 */ willCallRealMethod()391 BDDStubber willCallRealMethod(); 392 393 /** 394 * See original {@link Stubber#when(Object)} 395 * @since 1.8.0 396 */ given(T mock)397 <T> T given(T mock); 398 } 399 400 private static class BDDStubberImpl implements BDDStubber { 401 402 private final Stubber mockitoStubber; 403 BDDStubberImpl(Stubber mockitoStubber)404 public BDDStubberImpl(Stubber mockitoStubber) { 405 this.mockitoStubber = mockitoStubber; 406 } 407 given(T mock)408 public <T> T given(T mock) { 409 return mockitoStubber.when(mock); 410 } 411 willAnswer(Answer<?> answer)412 public BDDStubber willAnswer(Answer<?> answer) { 413 return new BDDStubberImpl(mockitoStubber.doAnswer(answer)); 414 } 415 will(Answer<?> answer)416 public BDDStubber will(Answer<?> answer) { 417 return new BDDStubberImpl(mockitoStubber.doAnswer(answer)); 418 } 419 420 /** 421 * @deprecated please use {@link #willDoNothing()} instead 422 */ 423 @Deprecated willNothing()424 public BDDStubber willNothing() { 425 return willDoNothing(); 426 } 427 willDoNothing()428 public BDDStubber willDoNothing() { 429 return new BDDStubberImpl(mockitoStubber.doNothing()); 430 } 431 willReturn(Object toBeReturned)432 public BDDStubber willReturn(Object toBeReturned) { 433 return new BDDStubberImpl(mockitoStubber.doReturn(toBeReturned)); 434 } 435 willReturn(Object toBeReturned, Object... nextToBeReturned)436 public BDDStubber willReturn(Object toBeReturned, Object... nextToBeReturned) { 437 return new BDDStubberImpl(mockitoStubber.doReturn(toBeReturned).doReturn(nextToBeReturned)); 438 } 439 willThrow(Throwable... toBeThrown)440 public BDDStubber willThrow(Throwable... toBeThrown) { 441 return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown)); 442 } 443 willThrow(Class<? extends Throwable> toBeThrown)444 public BDDStubber willThrow(Class<? extends Throwable> toBeThrown) { 445 return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown)); 446 } 447 willThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown)448 public BDDStubber willThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown) { 449 return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown, nextToBeThrown)); 450 } 451 willCallRealMethod()452 public BDDStubber willCallRealMethod() { 453 return new BDDStubberImpl(mockitoStubber.doCallRealMethod()); 454 } 455 } 456 457 /** 458 * see original {@link Mockito#doThrow(Throwable[])} 459 * @since 2.1.0 460 */ willThrow(Throwable... toBeThrown)461 public static BDDStubber willThrow(Throwable... toBeThrown) { 462 return new BDDStubberImpl(Mockito.doThrow(toBeThrown)); 463 } 464 465 /** 466 * see original {@link Mockito#doThrow(Class)} 467 * @since 1.9.0 468 */ willThrow(Class<? extends Throwable> toBeThrown)469 public static BDDStubber willThrow(Class<? extends Throwable> toBeThrown) { 470 return new BDDStubberImpl(Mockito.doThrow(toBeThrown)); 471 } 472 473 /** 474 * see original {@link Mockito#doThrow(Class)} 475 * @since 1.9.0 476 */ willThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... throwableTypes)477 public static BDDStubber willThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... throwableTypes) { 478 return new BDDStubberImpl(Mockito.doThrow(toBeThrown, throwableTypes)); 479 } 480 481 /** 482 * see original {@link Mockito#doAnswer(Answer)} 483 * @since 1.8.0 484 */ willAnswer(Answer<?> answer)485 public static BDDStubber willAnswer(Answer<?> answer) { 486 return new BDDStubberImpl(Mockito.doAnswer(answer)); 487 } 488 489 /** 490 * see original {@link Mockito#doAnswer(Answer)} 491 * @since 2.1.0 492 */ will(Answer<?> answer)493 public static BDDStubber will(Answer<?> answer) { 494 return new BDDStubberImpl(Mockito.doAnswer(answer)); 495 } 496 497 /** 498 * see original {@link Mockito#doNothing()} 499 * @since 1.8.0 500 */ willDoNothing()501 public static BDDStubber willDoNothing() { 502 return new BDDStubberImpl(Mockito.doNothing()); 503 } 504 505 /** 506 * see original {@link Mockito#doReturn(Object)} 507 * @since 1.8.0 508 */ willReturn(Object toBeReturned)509 public static BDDStubber willReturn(Object toBeReturned) { 510 return new BDDStubberImpl(Mockito.doReturn(toBeReturned)); 511 } 512 513 /** 514 * see original {@link Mockito#doReturn(Object, Object...)} 515 * @since 2.1.0 516 */ 517 @SuppressWarnings({"unchecked", "varargs"}) willReturn(Object toBeReturned, Object... toBeReturnedNext)518 public static BDDStubber willReturn(Object toBeReturned, Object... toBeReturnedNext) { 519 return new BDDStubberImpl(Mockito.doReturn(toBeReturned, toBeReturnedNext)); 520 } 521 522 /** 523 * see original {@link Mockito#doCallRealMethod()} 524 * @since 1.8.0 525 */ willCallRealMethod()526 public static BDDStubber willCallRealMethod() { 527 return new BDDStubberImpl(Mockito.doCallRealMethod()); 528 } 529 } 530