java - with - mockito when not working

How to tell a Mockito mock object to return something different the next time it is called? (4)

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.

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 Anyone using spy() and the doReturn() instead of the when() method:

what you need to return different object on different calls is this:


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