2. Test Doubles
generic term for any kind of pretend object used in place of a real object for testing purposes
2
3. Dummy Object
objects are passed around but never actually used. Usually
they are just used to fill parameter lists
3
4. Fake Object
objects actually have working implementations, but usually take some shortcut which
makes them not suitable for production (an in memory database is a good example).
4
5. provide canned answers to calls made during the test, usually not responding at all to
anything outside what's programmed in for the test. Stubs may also record information
about calls, such as an email gateway stub that remembers the messages it 'sent', or
maybe only how many messages it 'sent'
Stub
5
6. an object with the ability to have a programmed expected behavior, and verify the
interactions occurring in its lifetime (this object is usually created with the help of
mocking framework)
Mock
6
7. a mock created as a proxy to an existing real object; some methods can be stubbed,
while the un- stubbed ones are for- warded to the covered object
Spy
7
9. creating mock objects
import org.mockito.Mockito;
Person person = Mockito.mock(Person.class);
or
import static org.mockito.Mockito.mock;
Person person = mock(Person.class);
9
using static method mock()
10. creating mock objects
10
using @Mock annotation
@RunWith(MockitoJUnitRunner.class)
public class ClassTest {
}
or
public class ClassTest{
MockitoAnnotations.initMocks(ClassTest.class);
}
11. creating mock objects
declaring an attribute with the @Mock annotation
public class ClassTest {
@Mock
private Person person;
11
}
12. requesting specific
behaviors
Method Description
thenReturn(T valueToBeReturned) returns given value
thenThrow(Throwable toBeThrown)
thenThrow(Class<? extends Throwable> toBeThrown)
12
throws given exception
then(Answer answer)
thenAnswer(Answer answer)
uses user created code to answer
thenCallRealMethod() calls real method when working with
partial mock/spy
13. stubbing method
public class SimpleStubbingTest {
public static final int TEST_NUMBER_OF_RELATIVES = 5;
@Test
public void shouldReturnGivenValue() {
Person person = mock(Person.class);
when(person.getNumberOfRelatives())
.thenReturn(TEST_NUMBER_OF_RELATIVES);
int numberOfRelatives = person.getNumberOfRelatives();
assertEquals(numberOfRelatives, TEST_NUMBER_OF_RELATIVES);
13
}
}
14. BDDMockito
given - when - then
@Test
public void shouldReturnGivenValueUsingBDDSemantics() {
// given
Person person = mock(Person.class);
given(person.getNumberOfRelatives()).willReturn(
TEST_NUMBER_OF_RELATIVES);
// when
int numberOfRelatives = person.getNumberOfRelatives();
// then
assertEquals(numberOfRelatives, TEST_NUMBER_OF_RELATIVES);
14
}
15. stubbing multiples calls
to the same method
@Test
public void shouldReturnLastDefinedValue() {
Weather weather = mock(Weather.class);
given(weather.getTemperature()).willReturn( 10, 12, 23 );
assertEquals(weather.getTemperature(), 10);
assertEquals(weather.getTemperature(), 12);
assertEquals(weather.getTemperature(), 23);
assertEquals(weather.getTemperature(), 23);
15
}
17. custom answer
public class ReturnCustomAnswer implements Answer<Object> {
public Object answer(InvocationOnMock invocation)
17
throws Throwable {
return null;
}
}
18. verifying behavior
Method Description
times(int wantedNumberOfInvocations) called exactly n times (one by default)
never() never called
atLeastOnce() called at least once
atLeast(int minNumberOfInvocations) called at least n times
atMost(int maxNumberOfInvocations) called at most n times
only() the only method called on a mock
timeout(int millis) interacted in a specified time range
18
20. another verifications
InOrder API
verifying the call order
ArgumentCaptor
argument matching
timeout
verify(mockObject, timeout(10)).getTemperature();
20
21. some limitations
• mock final classes
• mock enums
• mock final methods
• mock static methods
• mock private methods
• mock hashCode() and equals()
21
https://code.google.com/p/powermock/
will help you!