|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.mockito.Matchers org.mockito.Mockito org.mockito.BDDMockito
public class 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);
One of the purposes of BDDMockito is also to show how to tailor the mocking syntax to a different programming style.
Nested Class Summary | |
---|---|
static interface |
BDDMockito.BDDMyOngoingStubbing<T>
See original OngoingStubbing |
static class |
BDDMockito.BDDOngoingStubbingImpl<T>
|
static interface |
BDDMockito.BDDStubber
See original Stubber |
static class |
BDDMockito.BDDStubberImpl
|
Field Summary |
---|
Fields inherited from class org.mockito.Mockito |
---|
CALLS_REAL_METHODS, RETURNS_DEEP_STUBS, RETURNS_DEFAULTS, RETURNS_MOCKS, RETURNS_SMART_NULLS |
Constructor Summary | |
---|---|
BDDMockito()
|
Method Summary | ||
---|---|---|
static
|
given(T methodCall)
see original Mockito.when(Object) |
|
static BDDMockito.BDDStubber |
willAnswer(Answer answer)
see original Mockito.doAnswer(Answer) |
|
static BDDMockito.BDDStubber |
willCallRealMethod()
see original Mockito.doCallRealMethod() |
|
static BDDMockito.BDDStubber |
willDoNothing()
see original Mockito.doNothing() |
|
static BDDMockito.BDDStubber |
willReturn(java.lang.Object toBeReturned)
see original Mockito.doReturn(Object) |
|
static BDDMockito.BDDStubber |
willThrow(java.lang.Class<? extends java.lang.Throwable> toBeThrown)
see original Mockito.doThrow(Throwable) |
|
static BDDMockito.BDDStubber |
willThrow(java.lang.Throwable toBeThrown)
see original Mockito.doThrow(Throwable) |
Methods inherited from class org.mockito.Mockito |
---|
atLeast, atLeastOnce, atMost, calls, doAnswer, doCallRealMethod, doNothing, doReturn, doThrow, doThrow, ignoreStubs, inOrder, mock, mock, mock, mock, mock, mockingDetails, never, only, reset, 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 |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public BDDMockito()
Method Detail |
---|
public static <T> BDDMockito.BDDMyOngoingStubbing<T> given(T methodCall)
Mockito.when(Object)
public static BDDMockito.BDDStubber willThrow(java.lang.Throwable toBeThrown)
Mockito.doThrow(Throwable)
public static BDDMockito.BDDStubber willThrow(java.lang.Class<? extends java.lang.Throwable> toBeThrown)
Mockito.doThrow(Throwable)
public static BDDMockito.BDDStubber willAnswer(Answer answer)
Mockito.doAnswer(Answer)
public static BDDMockito.BDDStubber willDoNothing()
Mockito.doNothing()
public static BDDMockito.BDDStubber willReturn(java.lang.Object toBeReturned)
Mockito.doReturn(Object)
public static BDDMockito.BDDStubber willCallRealMethod()
Mockito.doCallRealMethod()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |