A dive into Mockito – 1

Part 1 – Getting Started with Mockito

  • Mocking Frameworks – Mockito
  • A simple use of Mockito
  • Verifying Interactions with the mock
  • Stubbing methods using when()

Full Series

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.


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 {

	public void testCheckFormat() {
		HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class);

		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:

  1. Create a mock of the object on which the method under test is dependent [line 10]
  2. Specify behaviors on the mock object (stub) [line 11]
  3. 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:


It is also easy to ensure that there were no more interactions with our mock apart from getting the email:


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:


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!

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:


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”.


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!

whenOnly 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
  • Part 4 – Some complex examples and common mistakes in using Mockito

Share this article

One thought on “A dive into Mockito – 1”

Comments are closed.