Home | History | Annotate | Download | only in mockito
      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          */
     93         BDDMyOngoingStubbing<T> willAnswer(Answer<?> answer);
     94 
     95         /**
     96          * See original {@link OngoingStubbing#then(Answer)}
     97          * @since 1.9.0
     98          */
     99         BDDMyOngoingStubbing<T> will(Answer<?> answer);
    100 
    101         /**
    102          * See original {@link OngoingStubbing#thenReturn(Object)}
    103          * @since 1.8.0
    104          */
    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"})
    112         BDDMyOngoingStubbing<T> willReturn(T value, T... values);
    113 
    114         /**
    115          * See original {@link OngoingStubbing#thenThrow(Throwable...)}
    116          * @since 1.8.0
    117          */
    118         BDDMyOngoingStubbing<T> willThrow(Throwable... throwables);
    119 
    120         /**
    121          * See original {@link OngoingStubbing#thenThrow(Class)}
    122          * @since 2.1.0
    123          */
    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"})
    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          */
    138         BDDMyOngoingStubbing<T> willCallRealMethod();
    139 
    140         /**
    141          * See original {@link OngoingStubbing#getMock()}
    142          * @since 1.9.0
    143          */
    144         <M> M getMock();
    145     }
    146 
    147     private static class BDDOngoingStubbingImpl<T> implements BDDMyOngoingStubbing<T> {
    148 
    149         private final OngoingStubbing<T> mockitoOngoingStubbing;
    150 
    151         public BDDOngoingStubbingImpl(OngoingStubbing<T> ongoingStubbing) {
    152             this.mockitoOngoingStubbing = ongoingStubbing;
    153         }
    154 
    155         public BDDMyOngoingStubbing<T> willAnswer(Answer<?> answer) {
    156             return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenAnswer(answer));
    157         }
    158 
    159         public BDDMyOngoingStubbing<T> will(Answer<?> answer) {
    160             return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.then(answer));
    161         }
    162 
    163         public BDDMyOngoingStubbing<T> willReturn(T value) {
    164             return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenReturn(value));
    165         }
    166 
    167         public BDDMyOngoingStubbing<T> willReturn(T value, T... values) {
    168             return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenReturn(value, values));
    169         }
    170 
    171         public BDDMyOngoingStubbing<T> willThrow(Throwable... throwables) {
    172             return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenThrow(throwables));
    173         }
    174 
    175         public BDDMyOngoingStubbing<T> willThrow(Class<? extends Throwable> throwableType) {
    176             return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenThrow(throwableType));
    177         }
    178 
    179         public BDDMyOngoingStubbing<T> willThrow(Class<? extends Throwable> throwableType, Class<? extends Throwable>... throwableTypes) {
    180             return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenThrow(throwableType, throwableTypes));
    181         }
    182 
    183         public BDDMyOngoingStubbing<T> willCallRealMethod() {
    184             return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenCallRealMethod());
    185         }
    186 
    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      */
    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      */
    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          */
    231         T should();
    232 
    233         /**
    234          * @see #verify(Object, VerificationMode)
    235          * @since 1.10.5
    236          */
    237         T should(VerificationMode mode);
    238 
    239         /**
    240          * @see InOrder#verify(Object)
    241          * @since 2.1.0
    242          */
    243         T should(InOrder inOrder);
    244 
    245         /**
    246          * @see InOrder#verify(Object, VerificationMode)
    247          * @since 2.1.0
    248          */
    249         T should(InOrder inOrder, VerificationMode mode);
    250 
    251         /**
    252          * @see #verifyZeroInteractions(Object...)
    253          * @since 2.1.0
    254          */
    255         void shouldHaveZeroInteractions();
    256 
    257         /**
    258          * @see #verifyNoMoreInteractions(Object...)
    259          * @since 2.1.0
    260          */
    261         void shouldHaveNoMoreInteractions();
    262     }
    263 
    264     private static class ThenImpl<T> implements Then<T> {
    265 
    266         private final T mock;
    267 
    268         ThenImpl(T mock) {
    269             this.mock = mock;
    270         }
    271 
    272         /**
    273          * @see #verify(Object)
    274          * @since 1.10.5
    275          */
    276         public T should() {
    277             return verify(mock);
    278         }
    279 
    280         /**
    281          * @see #verify(Object, VerificationMode)
    282          * @since 1.10.5
    283          */
    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          */
    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          */
    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          */
    308         public void shouldHaveZeroInteractions() {
    309             verifyZeroInteractions(mock);
    310         }
    311 
    312         /**
    313          * @see #verifyNoMoreInteractions(Object...)
    314          * @since 2.1.0
    315          */
    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          */
    330         BDDStubber willAnswer(Answer<?> answer);
    331 
    332         /**
    333          * See original {@link Stubber#doAnswer(Answer)}
    334          * @since 1.8.0
    335          */
    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
    347         BDDStubber willNothing();
    348 
    349         /**
    350          * See original {@link Stubber#doNothing()}
    351          * @since 1.10.20
    352          */
    353         BDDStubber willDoNothing();
    354 
    355         /**
    356          * See original {@link Stubber#doReturn(Object)}
    357          * @since 2.1.0
    358          */
    359         BDDStubber willReturn(Object toBeReturned);
    360 
    361         /**
    362          * See original {@link Stubber#doReturn(Object)}
    363          * @since 2.1.0
    364          */
    365         @SuppressWarnings({"unchecked", "varargs"})
    366         BDDStubber willReturn(Object toBeReturned, Object... nextToBeReturned);
    367 
    368         /**
    369          * See original {@link Stubber#doThrow(Throwable...)}
    370          * @since 1.8.0
    371          */
    372         BDDStubber willThrow(Throwable... toBeThrown);
    373 
    374         /**
    375          * See original {@link Stubber#doThrow(Class)}
    376          * @since 2.1.0
    377          */
    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"})
    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          */
    391         BDDStubber willCallRealMethod();
    392 
    393         /**
    394          * See original {@link Stubber#when(Object)}
    395          * @since 1.8.0
    396          */
    397         <T> T given(T mock);
    398     }
    399 
    400     private static class BDDStubberImpl implements BDDStubber {
    401 
    402         private final Stubber mockitoStubber;
    403 
    404         public BDDStubberImpl(Stubber mockitoStubber) {
    405             this.mockitoStubber = mockitoStubber;
    406         }
    407 
    408         public <T> T given(T mock) {
    409             return mockitoStubber.when(mock);
    410         }
    411 
    412         public BDDStubber willAnswer(Answer<?> answer) {
    413             return new BDDStubberImpl(mockitoStubber.doAnswer(answer));
    414         }
    415 
    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
    424         public BDDStubber willNothing() {
    425             return willDoNothing();
    426         }
    427 
    428         public BDDStubber willDoNothing() {
    429             return new BDDStubberImpl(mockitoStubber.doNothing());
    430         }
    431 
    432         public BDDStubber willReturn(Object toBeReturned) {
    433             return new BDDStubberImpl(mockitoStubber.doReturn(toBeReturned));
    434         }
    435 
    436         public BDDStubber willReturn(Object toBeReturned, Object... nextToBeReturned) {
    437             return new BDDStubberImpl(mockitoStubber.doReturn(toBeReturned).doReturn(nextToBeReturned));
    438         }
    439 
    440         public BDDStubber willThrow(Throwable... toBeThrown) {
    441             return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown));
    442         }
    443 
    444         public BDDStubber willThrow(Class<? extends Throwable> toBeThrown) {
    445             return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown));
    446         }
    447 
    448         public BDDStubber willThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown) {
    449             return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown, nextToBeThrown));
    450         }
    451 
    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      */
    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      */
    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      */
    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      */
    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      */
    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      */
    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      */
    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"})
    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      */
    526     public static BDDStubber willCallRealMethod() {
    527         return new BDDStubberImpl(Mockito.doCallRealMethod());
    528     }
    529 }
    530