Class BDDMockito
- java.lang.Object
-
- org.mockito.ArgumentMatchers
-
- org.mockito.Mockito
-
- org.mockito.BDDMockito
-
public class BDDMockito extends Mockito
Behavior Driven Development style of writing tests uses //given //when //then comments as fundamental parts of your test methods. This is exactly how we write our tests and we warmly encourage you to do so!Start learning about BDD here: https://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 withgiven(Object)
method. Now it really nicely integrates with the given component of a BDD style test!Here is how the test might look like:
Stubbing voids with throwables: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()); }
//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.
- Since:
- 1.8.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
BDDMockito.BDDMyOngoingStubbing<T>
See originalOngoingStubbing
static interface
BDDMockito.BDDStubber
See originalStubber
static interface
BDDMockito.Then<T>
Provides fluent way of mock verification.
-
Field Summary
-
Fields inherited from class org.mockito.Mockito
CALLS_REAL_METHODS, RETURNS_DEEP_STUBS, RETURNS_DEFAULTS, RETURNS_MOCKS, RETURNS_SELF, RETURNS_SMART_NULLS
-
-
Constructor Summary
Constructors Constructor Description BDDMockito()
-
Method Summary
-
Methods inherited from class org.mockito.Mockito
after, atLeast, atLeastOnce, atMost, atMostOnce, calls, clearAllCaches, clearInvocations, description, doAnswer, doCallRealMethod, doNothing, doReturn, doReturn, doThrow, doThrow, doThrow, framework, ignoreStubs, inOrder, lenient, mock, mock, mock, mock, mockConstruction, mockConstruction, mockConstruction, mockConstruction, mockConstruction, mockConstruction, mockConstructionWithAnswer, mockingDetails, mockitoSession, mockStatic, mockStatic, mockStatic, mockStatic, never, only, reset, spy, spy, timeout, times, validateMockitoUsage, verify, verify, verifyNoInteractions, verifyNoMoreInteractions, when, withSettings
-
Methods inherited from class org.mockito.ArgumentMatchers
any, any, anyBoolean, anyByte, anyChar, anyCollection, anyDouble, anyFloat, anyInt, anyIterable, anyList, anyLong, anyMap, anySet, anyShort, anyString, argThat, booleanThat, byteThat, charThat, contains, doubleThat, endsWith, eq, eq, eq, eq, eq, eq, eq, eq, eq, floatThat, intThat, isA, isNotNull, isNull, longThat, matches, matches, notNull, nullable, refEq, same, shortThat, startsWith
-
-
-
-
Method Detail
-
given
public static <T> BDDMockito.BDDMyOngoingStubbing<T> given(T methodCall)
see originalMockito.when(Object)
- Since:
- 1.8.0
-
then
public static <T> BDDMockito.Then<T> then(T mock)
Bdd style verification of mock behavior.person.ride(bike); person.ride(bike); then(person).should(times(2)).ride(bike);
- Since:
- 1.10.0
- See Also:
Mockito.verify(Object)
,Mockito.verify(Object, VerificationMode)
-
willThrow
public static BDDMockito.BDDStubber willThrow(Throwable... toBeThrown)
see originalMockito.doThrow(Throwable[])
- Since:
- 2.1.0
-
willThrow
public static BDDMockito.BDDStubber willThrow(Class<? extends Throwable> toBeThrown)
see originalMockito.doThrow(Class)
- Since:
- 1.9.0
-
willThrow
public static BDDMockito.BDDStubber willThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... throwableTypes)
see originalMockito.doThrow(Class)
- Since:
- 1.9.0
-
willAnswer
public static BDDMockito.BDDStubber willAnswer(Answer<?> answer)
see originalMockito.doAnswer(Answer)
- Since:
- 1.8.0
-
will
public static BDDMockito.BDDStubber will(Answer<?> answer)
see originalMockito.doAnswer(Answer)
- Since:
- 2.1.0
-
willDoNothing
public static BDDMockito.BDDStubber willDoNothing()
see originalMockito.doNothing()
- Since:
- 1.8.0
-
willReturn
public static BDDMockito.BDDStubber willReturn(Object toBeReturned)
see originalMockito.doReturn(Object)
- Since:
- 1.8.0
-
willReturn
public static BDDMockito.BDDStubber willReturn(Object toBeReturned, Object... toBeReturnedNext)
see originalMockito.doReturn(Object, Object...)
- Since:
- 2.1.0
-
willCallRealMethod
public static BDDMockito.BDDStubber willCallRealMethod()
see originalMockito.doCallRealMethod()
- Since:
- 1.8.0
-
-