Mockito/Powermock: Presented By: Bello Muhammad
Mockito/Powermock: Presented By: Bello Muhammad
● Test Double
● Why Mock
● Mockito Installation
Using Mockito
● Dummy objects
● Fake objects
● Stubs objects
● Spies objects
● Mocks objects
Why Mock?
Most of the time the code we write have
dependencies. Often, delegates some work to
other methods in other classes.
// Gradle Dependency
testCompile “org.mockito:mockito−core:1.9.5“
// Ivy Dependency
<dependency org=”org.mockito” name=”mockito-core” rev=”1.9.5” conf=”test->default”/>
Using Mockito
After adding the dependencies in our project what
next?
How to Inject Mocks?
Assuming we want to test a class in our
application, we first create and inject the
dependencies using either annotations or
Mockito's static method mock().
private EtcBaseDao etcBaseDao;
private EtcBaseService etcBaseService;
@Before
public void setUp() {
etcBaseDao = Mockito.mock(EtcBaseDao.class);
etcBaseService = new EtcBaseService(etcBaseDao);
}
Using @Mock and @InjectMocks
There are two commonly used annotations:
1. @Mock
2. @InjectMocks
@Mock
private EtcBaseDao etcBaseDao;
@InjectMocks
private EtcBaseService etcBaseService;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
Tired of Using initMocks(this)?
Using runners from Mockito or from Spring
Framework will give you automatic validation and
initMocks.
● MockitoJUnitRunner
● MockitoJUnit4Runner
@Runwith(MockitoJUnit4Runner.class)
public class MockitoJUnit4RunnerTest {
@Mock
private EtcBaseDao etcBaseDao;
@InjectMocks
private EtcBaseService etcBaseService;
}
Stubbing Method's Return Value
The ability to return a test double as value when a
method is called is called Stubbing. Using
Mockito’s when() with thenReturn() you can
specify how and what a method should return.
The pattern:
● Arrange
● Act
● assert
Mocking Methods with Mockito
After creating and injecting your mock, you should then tell Mockito how to behave when certain
method are invoked.
Mockito.when(instanceName.methodName(methodArguments)).thenReturn(true);
Mockito.when(etcBaseService.save(etcBase)).thenReturn(etcBase);
@Test(expected = ExceptionName.class)
@Test(expected = ExceptionClassName.class)
public void theNameOfOurTestMethod() {
com.etcbase.mockito.service.MockitoExpectedTest.java
Mocking Void Methods with Mockito
With Mockito we can use doAnswer() to mock a
void method, doThrow() to throw an exception
from a void method. The following example
illustrate that.
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock)
throws Throwable {
return null;
}}).when(etcBaseDao).delete(Mockito.any(Long.class));
com.etcbase.mockito.service.MockitoVoidTest.java
Using Verify with Mockito
Apart from asserting that the return values are
valid, we can also verify that a given method is
called on a given mock object during test
execution, most especially when the method
under test is a Void method.
com.etcbase.mockito.service.MockitoWithVerifyTest.java
Using ArgumentCaptor
The argumentcaptor allows you to capture any argument that is
passed into a mock method. Mockito.ArgumentCaptor.
Usage:
@Captor
Private ArgumentCaptor<EtcBase> etcBaseArgumentCaptor;
com.etcbase.mockito.service.MockitoArgumentCaptorTest.java
Mockito Spy
Sometimes we do want to interact with the real
service and verify that it was invoked, that is
where Mockito spy is at your back.
Usage:
@Spy // instead of using @Mock we use @Spy
private EtcBaseDao etcBaseDao;
Using MockitoJUnitRunner
Are you tired of getting meaning less error? Or
verification errors? Again..., initMocks. With
Mockito MockitoJUnitRunner give you get
automatic validation and automatic initMocks().
@Test
public void successAutomaticValidation() {
when(myMock.method1());
// Do something here