54

I'm using the Mockito framework to create Mock objects in my JUnit tests. Each mock knows what methods have been called on it, so during my tests I can write

verify(myMock, atLeastOnce()).myMethod();

I am wondering if this internal mock knowledge of what it has called will persist across my tests? If it does persist, then I could be getting false positives when using the same verify method in two tests.

A code example

@RunWith(MockitoJUnitRunner.class)
public class EmrActivitiesImplTest {
    
    @Mock private MyClass myMock;
    
    @Before
    public void setup() {
        when(myMock.myMethod()).thenReturn("hello");
    }
    
    @Test
    public void test1() {
        // ..some logic
        verify(myMock, atLeastOnce()).myMethod();
    }
    
    @Test
    public void test2() {
        // ..some other logic
        verify(myMock, atLeastOnce()).myMethod();
    }  
}

Mock state is persisted - test2 will pass regardless, since test1's verify method passed

Mock state is reset - test2 will fail if myMock.myMethod() isn't called

2
  • If I'm not wrong, setup is called once, before all tests... so yes, all your tests are using the same mock object. You'd need to create that object at the beginning of each test method May 29, 2013 at 14:04
  • Actually, the @Before annotation will call setup() before each test case, not just once before all the tests. Aug 7, 2018 at 23:55

3 Answers 3

61

JUnit creates a new instance of test class each time it runs a new test method and runs @Before method each time it creates a new test class. You can easily test it:

@Before
public void setup() {
    System.out.println("setup");
    when(myMock.myMethod()).thenReturn("hello");
}

And MockitoJUnitRunner will create a new MyMock mock instance for every test method.

4
  • 19
    This is correct. JUnit designers wanted test isolation between test methods, so it creates a new instance of the test class to run each test method. Note that TestNG creator took a different way, and chose isolation per test class, that means there is no isolation between test methods of a TestNG test. Hence instance mocks might need to be reseted between each test method.
    – bric3
    May 29, 2013 at 15:22
  • 2
    And this is a big problem when using singletons, because the mocks only will be correct for the first test... Apr 12, 2017 at 1:32
  • 4
    Have you found any place where this is documented? I have known this to be true for a while but after having someone ask I have been searching and can't seem to find it in the doc anywhere.
    – dsingleton
    Jul 20, 2017 at 20:51
  • @dsingleton The nearest think I have found is docs about the introduction of the reset method but that is mainly aimed to be used by container-injected mocks: javadoc.io/static/org.mockito/mockito-core/3.3.3/org/mockito/… and github.com/mockito/mockito/wiki/FAQ
    – D-Dᴙum
    Apr 30, 2020 at 8:15
19
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class sandbox {

    @Mock
    private MyClass myMock;

    @Before
    public void setup() {
        when(myMock.myMethod()).thenReturn("hello");
    }

    @Test
    public void test1() {
        myMock.myMethod();
        verify(myMock, times(1)).myMethod();
    }

    @Test
    public void test2() {
        myMock.myMethod();
        verify(myMock, times(1)).myMethod();
    }

}

This passes. If the state persisted then the second test would fail. If you debug it you would see that you get a new instance of the mocked object for each test.

1
  • 2
    Yes, agree, if @RunWith(MockitoJUnitRunner.class) is used, new mocks will be injected.
    – yuranos
    Jun 13, 2016 at 23:46
1

If you just initialize the Mock objects in your setup, then inside each test you can provide different functionality. You can initialize it once and change how they act/expect per test after that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.