Package org.mockito
Class BDDMockito
java.lang.Object
org.mockito.Matchers
org.mockito.Mockito
org.mockito.BDDMockito
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: 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);
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 ClassesModifier and TypeClassDescriptionstatic interface
See originalOngoingStubbing
static class
Deprecated.static interface
See originalStubber
static class
Deprecated.not part of the public API, useBDDMockito.BDDStubber
instead.static interface
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_SMART_NULLS
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> BDDMockito.BDDMyOngoingStubbing
<T> given
(T methodCall) see originalMockito.when(Object)
static <T> BDDMockito.Then
<T> then
(T mock) Bdd style verification of mock behavior.static BDDMockito.BDDStubber
willAnswer
(Answer answer) see originalMockito.doAnswer(Answer)
static BDDMockito.BDDStubber
see originalMockito.doCallRealMethod()
static BDDMockito.BDDStubber
see originalMockito.doNothing()
static BDDMockito.BDDStubber
willReturn
(Object toBeReturned) see originalMockito.doReturn(Object)
static BDDMockito.BDDStubber
see originalMockito.doThrow(Throwable)
static BDDMockito.BDDStubber
see originalMockito.doThrow(Throwable)
Methods inherited from class org.mockito.Mockito
after, atLeast, atLeastOnce, atMost, calls, doAnswer, doCallRealMethod, doNothing, doReturn, doThrow, doThrow, ignoreStubs, inOrder, mock, mock, mock, mock, mock, mockingDetails, never, only, reset, spy, spy, stub, stubVoid, timeout, times, validateMockitoUsage, verify, verify, verifyNoMoreInteractions, verifyZeroInteractions, when, withSettings
Methods inherited from class org.mockito.Matchers
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
-
Constructor Details
-
BDDMockito
public BDDMockito()
-
-
Method Details
-
given
see originalMockito.when(Object)
- Since:
- 1.8.0
-
then
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:
-
willThrow
see originalMockito.doThrow(Throwable)
- Since:
- 1.8.0
-
willThrow
see originalMockito.doThrow(Throwable)
- Since:
- 1.9.0
-
willAnswer
see originalMockito.doAnswer(Answer)
- Since:
- 1.8.0
-
willDoNothing
see originalMockito.doNothing()
- Since:
- 1.8.0
-
willReturn
see originalMockito.doReturn(Object)
- Since:
- 1.8.0
-
willCallRealMethod
see originalMockito.doCallRealMethod()
- Since:
- 1.8.0
-
BDDMockito.BDDMyOngoingStubbing
instead.