Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

This cookbook shows how to use Mockito to configure behavior in a variety of examples and use cases.

The format of the cookbook is example focused and practical — no extraneous details and explanations necessary.

And of course, if you want to learn more about testing well with Mockito, have a look at the other Mockito articles here.

Further reading:

Mockito Verify Cookbook

Mockito Verify examples, usage and best practices.

Mockito – Using Spies

Making good use of Spies in Mockito, and how spies are different from mocks.

Mockito's Mock Methods

This tutorial illustrates various uses of the standard static mock methods of the Mockito API.

We're going to be mocking a simple list implementation, which is the same implementation we used in the previous cookbook:

public class MyList extends AbstractList<String> {

    @Override
    public String get(final int index) {
        return null;
    }
    @Override
    public int size() {
        return 1;
    }
}

2. The Cookbook

Configure simple return behavior for mock:

MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false);

boolean added = listMock.add(randomAlphabetic(6));
assertThat(added, is(false));

Configure return behavior for mock in an alternative way:

MyList listMock = Mockito.mock(MyList.class);
doReturn(false).when(listMock).add(anyString());

boolean added = listMock.add(randomAlphabetic(6));
assertThat(added, is(false));

Configure mock to throw an exception on a method call:

@Test(expected = IllegalStateException.class)
public void givenMethodIsConfiguredToThrowException_whenCallingMethod_thenExceptionIsThrown() {
    MyList listMock = Mockito.mock(MyList.class);
    when(listMock.add(anyString())).thenThrow(IllegalStateException.class);

    listMock.add(randomAlphabetic(6));
}

Configure the behavior of a method with void return type — to throw an exception:

MyList listMock = Mockito.mock(MyList.class);
doThrow(NullPointerException.class).when(listMock).clear();

listMock.clear();

Configure the behavior of multiple calls:

MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString()))
  .thenReturn(false)
  .thenThrow(IllegalStateException.class);

listMock.add(randomAlphabetic(6));
listMock.add(randomAlphabetic(6)); // will throw the exception

Configure the behavior of a spy:

MyList instance = new MyList();
MyList spy = Mockito.spy(instance);

doThrow(NullPointerException.class).when(spy).size();
spy.size(); // will throw the exception

Configure method to call the real, underlying method on a mock:

MyList listMock = Mockito.mock(MyList.class);
when(listMock.size()).thenCallRealMethod();

assertThat(listMock.size(), equalTo(1));

Configure mock method call with custom Answer:

MyList listMock = Mockito.mock(MyList.class);
doAnswer(invocation -> "Always the same").when(listMock).get(anyInt());

String element = listMock.get(1);
assertThat(element, is(equalTo("Always the same")));

3. Conclusion

The goal of this guide is to have this information readily available online. I've published a few similar development cookbooks on Google Guava and Hamcrest and now Mockito.

The implementation of all these examples and code snippets can be found over on GitHub. This is a Maven-based project, so it should be easy to import and run as it is.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Junit footer banner
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!