java doreturn - How to tell a Mockito mock object to return something different the next time it is called?

tutorial working (5)

So, I'm creating a mock object as a static variable on the class level like so... In one test, I want Foo.someMethod() to return a certain value, while in another test, I want it to return a different value. The problem I'm having is that it seems I need to rebuild the mocks to get this to work correctly. I'd like to avoid rebuilding the mocks, and just use the same objects in each test.

class TestClass {

    private static Foo mockFoo;

    public static void setUp() {
        mockFoo = mock(Foo.class);

    public void test1() {

        TestObject testObj = new TestObject(mockFoo);; // calls mockFoo.someMethod(), receiving 0 as the value


    public void test2() {

        TestObject testObj = new TestObject(mockFoo);; // calls mockFoo.someMethod(), STILL receiving 0 as the value, instead of expected 1.



In the second test, I'm still receiving 0 as the value when is called... What is the best way to resolve this? Note that I know I could use a different mock of Foo in each test, however, I have to chain multiple requests off of mockFoo, meaning I'd have to do the chaining in each test.


For all who search to return something and then for another call throw exception:

            .thenThrow(new RuntimeException("Fail"));


            .thenReturn(obj1, obj2)
            .thenThrow(new RuntimeException("Fail"));

First of all don't make the mock static. Make it a private field. Just put your setUp class in the @Before not @BeforeClass. It might be run a bunch, but it's cheap.

Secondly, the way you have it right now is the correct way to get a mock to return something different depending on the test.

Or, even cleaner:

when(mockFoo.someMethod()).thenReturn(obj1, obj2);

You could also Stub Consecutive Calls (#10 in 2.8.9 api). In this case, you would use multiple thenReturn calls or one thenReturn call with multiple parameters (varargs).

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;

public class TestClass {

    private Foo mockFoo;

    public void setup() {

    public void testFoo() {
        TestObject testObj = new TestObject(mockFoo);


    private void setupFoo() {
        mockFoo = mock(Foo.class);

            .thenReturn(-1); //any subsequent call will return -1

        // Or a bit shorter with varargs:
            .thenReturn(0, 1, -1); //any subsequent call will return -1

Since Mockito 2.0 there's also possibility to use static method Matchers.argThat(ArgumentMatcher). With the help of Java 8 it is now much cleaner and more readable to write:

verify(mockBar).doSth(argThat((arg) -> arg.getSurname().equals("OneSurname")));
verify(mockBar).doSth(argThat((arg) -> arg.getSurname().equals("AnotherSurname")));

If you're tied to lower Java version there's also not-that-bad:

verify(mockBar).doSth(argThat(new ArgumentMatcher<Employee>() {
        public boolean matches(Object emp) {
            return ((Employee) emp).getSurname().equals("SomeSurname");

Of course none of those can verify order of calls - for which you should use InOrder :

InOrder inOrder = inOrder(mockBar);

inOrder.verify(mockBar).doSth(argThat((arg) -> arg.getSurname().equals("FirstSurname")));
inOrder.verify(mockBar).doSth(argThat((arg) -> arg.getSurname().equals("SecondSurname")));

Please take a look at mockito-java8 project which makes possible to make calls such as:

verify(mockBar).doSth(assertArg(arg -> assertThat(arg.getSurname()).isEqualTo("Surname")));

java unit-testing junit mocking mockito