mockito intro
DESCRIPTION
introduction about mockito frameworkTRANSCRIPT
Cristian R. Silvaabout.me/ocristian
Test Doubles
generic term for any kind of pretend object used in place of a real object for testing purposes
2
Dummy Object
objects are passed around but never actually used. Usually
they are just used to fill parameter lists
3
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).
Fake Object
4
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
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
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
8
https://code.google.com/p/mockito/
creating mock objects
9
import org.mockito.Mockito;
Person person = Mockito.mock(Person.class);
or
import static org.mockito.Mockito.mock;
Person person = mock(Person.class);
using static method mock()
10
using @Mock annotation
@RunWith(MockitoJUnitRunner.class)public class ClassTest {}
or
public class ClassTest{MockitoAnnotations.initMocks(ClassTest.class);
}
creating mock objects
11
declaring an attribute with the @Mock annotation
creating mock objects
public class ClassTest {
@Mock private Person person;}
12
requesting specific behaviors
Method Description
thenReturn(T valueToBeReturned) returns given value
thenThrow(Throwable toBeThrown)
thenThrow(Class<? extends Throwable> toBeThrown)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);
}}
14
BDDMockito given - when - then
@Test public void shouldReturnGivenValueUsingBDDSemantics() {
// givenPerson person = mock(Person.class);given(person.getNumberOfRelatives()).willReturn(
TEST_NUMBER_OF_RELATIVES);
// whenint numberOfRelatives = person.getNumberOfRelatives();
// thenassertEquals(numberOfRelatives, TEST_NUMBER_OF_RELATIVES);
}
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);
}
16
stubbing void methods
@Test(expected = WeatherException.class) public void shouldStubVoidMethod() {
Weather weather = mock(Weather.class);
doThrow(WeatherException.class).when(weather).doSelfCheck();
//exception expectedweather.doSelfCheck();
}
17
custom answer
public class ReturnCustomAnswer implements Answer<Object> {
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;}
}
18
verifying behavior
Method Descriptiontimes(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
19
verifying behavior
verify(mockObject, never()).doSelfCheck(); verify(mockObject, times(2)).getNumberOfRelatives(); verify(mockObject, atLeast(1)).getTemperature();
20
another verifications
InOrder API verifying the call order
ArgumentCaptor argument matching
timeout verify(mockObject, timeout(10)).getTemperature();
21
some limitations
• mock final classes
• mock enums
• mock final methods
• mock static methods
• mock private methods
• mock hashCode() and equals()
will help you!
https://code.google.com/p/powermock/
references
22
https://code.google.com/p/mockito/
http://martinfowler.com/articles/mocksArentStubs.html
blog.caelum.com.br/facilitando-seus-testes-de-unidade-no-java-um-pouco-de-mockito/
http://refcardz.dzone.com/refcardz/mockito
23
?