org.mockito
public class BDDMockito extends Mockito
Start learning about BDD here: http://en.wikipedia.org/wiki/Behavior_Driven_Development
The problem is that current stubbing api with canonical role of when word does not integrate nicely with //given //when //then comments.
It's because stubbing belongs to given component of the test and not to the when component of the test.
Hence BDDMockito
class introduces an alias so that you stub method calls with given(Object)
method.
Now it really nicely integrates with the given component of a BDD style test!
Here is how the test might look like:
import static org.mockito.BDDMockito.*;
Seller seller = mock(Seller.class);
Shop shop = new Shop(seller);
public void shouldBuyBread() throws Exception {
//given
given(seller.askForBread()).willReturn(new Bread());
//when
Goods goods = shop.buyBread();
//then
assertThat(goods, containBread());
}
Stubbing voids with throwables:
//given
willThrow(new RuntimeException("boo")).given(mock).foo();
//when
Result result = systemUnderTest.perform();
//then
assertEquals(failure, result);
For BDD style mock verification take a look at BDDMockito.Then
in action:
person.ride(bike);
person.ride(bike);
then(person).should(times(2)).ride(bike);
then(person).shouldHaveNoMoreInteractions();
then(police).shouldHaveZeroInteractions();
It is also possible to do BDD style InOrder
verification:
InOrder inOrder = inOrder(person);
person.drive(car);
person.ride(bike);
person.ride(bike);
then(person).should(inOrder).drive(car);
then(person).should(inOrder, times(2)).ride(bike);
One of the purposes of BDDMockito is also to show how to tailor the mocking syntax to a different programming style.
Modifier and Type | Class and Description |
---|---|
static interface |
BDDMockito.BDDMyOngoingStubbing<T>
See original
OngoingStubbing |
static interface |
BDDMockito.BDDStubber
See original
Stubber |
static interface |
BDDMockito.Then<T>
Provides fluent way of mock verification.
|
CALLS_REAL_METHODS, RETURNS_DEEP_STUBS, RETURNS_DEFAULTS, RETURNS_MOCKS, RETURNS_SELF, RETURNS_SMART_NULLS
Constructor and Description |
---|
BDDMockito() |
after, atLeast, atLeastOnce, atMost, calls, clearInvocations, description, doAnswer, doCallRealMethod, doNothing, doReturn, doReturn, doThrow, doThrow, doThrow, ignoreStubs, inOrder, mock, mock, mock, mock, mockingDetails, never, only, reset, spy, spy, timeout, times, validateMockitoUsage, verify, verify, verifyNoMoreInteractions, verifyZeroInteractions, when, withSettings
any, any, anyBoolean, anyByte, anyChar, anyCollection, anyCollectionOf, anyDouble, anyFloat, anyInt, anyList, anyListOf, anyLong, anyMap, anyMapOf, anyObject, anySet, anySetOf, anyShort, anyString, anyVararg, argThat, booleanThat, byteThat, charThat, contains, doubleThat, endsWith, eq, eq, eq, eq, eq, eq, eq, eq, eq, floatThat, intThat, isA, isNotNull, isNotNull, isNull, isNull, longThat, matches, notNull, notNull, refEq, same, shortThat, startsWith
public static <T> BDDMockito.BDDMyOngoingStubbing<T> given(T methodCall)
Mockito.when(Object)
public static <T> BDDMockito.Then<T> then(T mock)
person.ride(bike);
person.ride(bike);
then(person).should(times(2)).ride(bike);
Mockito.verify(Object)
,
Mockito.verify(Object, VerificationMode)
public static BDDMockito.BDDStubber willThrow(Throwable... toBeThrown)
Mockito.doThrow(Throwable[])
public static BDDMockito.BDDStubber willThrow(Class<? extends Throwable> toBeThrown)
Mockito.doThrow(Class)
public static BDDMockito.BDDStubber willThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... throwableTypes)
Mockito.doThrow(Class)
public static BDDMockito.BDDStubber willAnswer(Answer<?> answer)
Mockito.doAnswer(Answer)
public static BDDMockito.BDDStubber will(Answer<?> answer)
Mockito.doAnswer(Answer)
public static BDDMockito.BDDStubber willDoNothing()
Mockito.doNothing()
public static BDDMockito.BDDStubber willReturn(Object toBeReturned)
Mockito.doReturn(Object)
public static BDDMockito.BDDStubber willReturn(Object toBeReturned, Object... toBeReturnedNext)
Mockito.doReturn(Object, Object...)
public static BDDMockito.BDDStubber willCallRealMethod()
Mockito.doCallRealMethod()