mockito intro

23
Cristian R. Silva about.me/ocristian

Upload: cristian-r-silva

Post on 06-Jul-2015

199 views

Category:

Software


2 download

DESCRIPTION

introduction about mockito framework

TRANSCRIPT

Page 2: Mockito intro

Test Doubles

generic term for any kind of pretend object used in place of a real object for testing purposes

2

Page 3: Mockito intro

Dummy Object

objects are passed around but never actually used. Usually

they are just used to fill parameter lists

3

Page 4: Mockito intro

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

Page 5: Mockito intro

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

Page 6: Mockito intro

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

Page 7: Mockito intro

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

Page 9: Mockito intro

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()

Page 10: Mockito intro

10

using @Mock annotation

@RunWith(MockitoJUnitRunner.class)public class ClassTest {}

or

public class ClassTest{MockitoAnnotations.initMocks(ClassTest.class);

}

creating mock objects

Page 11: Mockito intro

11

declaring an attribute with the @Mock annotation

creating mock objects

public class ClassTest {

@Mock private Person person;}

Page 12: Mockito intro

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

Page 13: Mockito intro

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);

}}

Page 14: Mockito intro

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);

}

Page 15: Mockito intro

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);

}

Page 16: Mockito intro

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();

}

Page 17: Mockito intro

17

custom answer

public class ReturnCustomAnswer implements Answer<Object> {

public Object answer(InvocationOnMock invocation) throws Throwable {

return null;}

}

Page 18: Mockito intro

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

Page 19: Mockito intro

19

verifying behavior

verify(mockObject, never()).doSelfCheck(); verify(mockObject, times(2)).getNumberOfRelatives(); verify(mockObject, atLeast(1)).getTemperature();

Page 20: Mockito intro

20

another verifications

InOrder API verifying the call order

ArgumentCaptor argument matching

timeout verify(mockObject, timeout(10)).getTemperature();

Page 21: Mockito intro

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/

Page 22: Mockito intro

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

Page 23: Mockito intro

23

?