unspoken reflections of my heart
A dive into Mockito – Part 1
Part 1 – Getting Started with Mockito
- Mocking Frameworks – Mockito
- A simple use of Mockito
- Verifying Interactions with the mock
- Stubbing methods using when()
Mocking Frameworks
Mocking Frameworks allow us to test the code you want, without its dependencies. In a unit test, mock objects can simulate the behaviour of complex objects that are impractical or impossible to incorporate into a unit test.
Mock objects isolate the unit of code you are testing.
Mockito
This is a framework used for test stubbing and interaction testing.
Let us assume that we have a simple servlet, and a method testCheckFormat() that checks the format of the email id submitted in the HTML form. It is impractical to create an actual HttpServletRequest object for unit test. So we mock this object, in order to test the code we are interested in.
A simple use of Mockito
import javax.servlet.http.HttpServletRequest;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
public class ServletFacadeTest {
@Test
public void testCheckFormat() {
HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class);
Mockito.when(requestMock.getAttribute("email")).thenReturn("xyz@abc.com");
ServletFacade servletFacade = new ServletFacade();
boolean result = servletFacade.checkFormat(requestMock);
Assert.assertEquals(true, result);
}
}
There are three fundamental steps in writing JUnit test cases with Mockito:
- Create a mock of the object on which the method under test is dependent [line 10]
- Specify behaviors on the mock object (stub) [line 11]
- Assert/verify test scenario [lines 14-16]
Verifying Interactions with the mock
One of the most important capabilities of Mockito is to verify whether the mock had any interactions. A mock object will remember all its invocations.
The following test would have passed in the above case:
Mockito.verify(requestMock).getAttribute("email");
It is also easy to ensure that there were no more interactions with our mock apart from getting the email:
Mockito.verify(requestMock).getAttribute("email");
Mockito.verifyNoMoreInteractions(requestMock);
Let’s assume that our logic now tries retrieving two details from the request, namely email and name. To test two invocations, we now can use:
Mockito.verify(requestMock).getAttribute("email");
Mockito.verify(requestMock).getAttribute("name");
We can write this slightly differently, using argument matchers provided by Mockito:
Mockito.verify(requestMock, Mockito.times(2)).getAttribute(Mockito.anyString());
Verifying a stubbed invocation is unnecessary and often leads to just redundant tests!
| METHODS FOR VERIFICATION | DESCRIPTION |
|---|---|
| verify( T mock).someMethod(); verify( T mock, times(int n) ).someMethod(); verify( T mock, atLeast(int n) ).someMethod(); verify( T mock, atLeastOnce() ).someMethod(); verify( T mock, atMost(int n) ).someMethod(); verify( T mock, never() ).someMethod(); | Verify that someMethod was called specified number of times. Default is once. |
| verifyZeroInteractions( Object... mocks ); | Verify that there have been no method calls on the mock object(s) |
| verifyNoMoreInteractions( Object... mocks ) | Verify that there have been no additional method calls on the mock object(s) besides what has already been verified. |
Stubbing methods using when()
Stubbing is used to specify method behaviors on mock objects.
We have already seen an example of stubbing:
Mockito.when(requestMock.getAttribute("email")).thenReturn("xyz@abc.com");
Argument matchers can be used in stubs to match the invocation with any specified argument:
In the below example, any invocation of getAttribute() with a String parameter will return the string “xyz@abc.com”.
Mockito.when(requestMock.getAttribute(Mockito.anyString()))
.thenReturn("xyz@abc.com");
Sometimes, we might have to test exception handling. How would my code handle if an exception is thrown by getAttribute()
Mockito.when(requestMock.getAttribute(Mockito.anyString())).thenThrow(new RuntimeException());
when() would work only for non-void methods!
| STUBBING WITH MOCKITO | USAGE |
|---|---|
| when | Only for non-void methods |
| when( mock.method() ).thenCallRealMethod(); when( mock.method() ).thenReturn(T value); when( mock.method() ).thenReturn(T value, T... values); when( mock.method() ).thenThrow(Throwable... throwables); when( mock.method() ).thenAnswer(Answer> answer); |
|
This should get you started with some basic unit tests with Mockito. Have fun !!
Future articles in the series:
- Part 2 – Argument Matchers and stubbing void methods
- Part 3 – Partial Mocks using Mockito and Limitations of Mockito
You might also like:
| Print article | This entry was posted by eskay on May 31, 2011 at 8:41 pm, and is filed under technology. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |





