Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resetting Mockito Spy

I have a test class (based on TestNG) where I use Mockito.verify for a spy object.

This works:

public class Program {
    @Spy
    private TestObject testObject;

    @Test
    public void test1() {
       testObject.makeSth(); 
       verify(testObject, only()).someMethodNeedToBeChecked(); 
    }
}

But here:

public class Program {
    @Spy
    private TestObject testObject;

    @Test
    public void test1() {
       testObject.makeSth(); 
       verify(testObject, only()).someMethodNeedToBeChecked(); 
    }

    @Test
    public void test2() {
        // Some different scenario
       testObject.makeSth(); 
       verify(testObject, only()).someMethodNeedToBeChecked(); 
        ...
    }
}

I get a Mokito exception that I have more that one invocation of someMethodNeedToBeChecked method. Of course I tried to add Mockito.reset(testObject) but it didn't help me at all.

How can I reset a spy object if I need to verify the same method in several tests?

like image 311
Alexander Bezrodniy Avatar asked Feb 26 '13 13:02

Alexander Bezrodniy


People also ask

What is Mockito Spy ()?

Simply put, the API is Mockito. spy() to spy on a real object. This will allow us to call all the normal methods of the object while still tracking every interaction, just as we would with a mock.

What is the difference between spy and mock in Mockito?

Mocks are used to create fully mock or dummy objects. It is mainly used in large test suites. Spies are used for creating partial or half mock objects. Like mock, spies are also used in large test suites.


1 Answers

From the Mockito documentation :

Resetting mocks (Since 1.8.0)

Smart Mockito users hardly use this feature because they know it could be a sign of poor tests. Normally, you don't need to reset your mocks, just create new mocks for each test method. Instead of reset() please consider writing simple, small and focused test methods over lengthy, over-specified tests. First potential code smell is reset() in the middle of the test method. This probably means you're testing too much. Follow the whisper of your test methods: "Please keep us small & focused on single behavior". There are several threads about it on mockito mailing list.

The only reason we added reset() method is to make it possible to work with container-injected mocks.

You should probably just recreate the spy in a @BeforeMethod :

public class Program {

    private TestObject testObject = new TestObject();
    private TestObject spyTestObject;

    @BeforeMethod
    public void buildSpy() {
        spyTestObject = spy(testObject);
    }

    @Test
    public void test1() {
       spyTestObject.makeSth(); 
       verify(spyTestObject , only()).someMethodNeedToBeChecked(); 
    }

    @Test
    public void test2() {
        // Some different scenario
       spyTestObject.makeSth(); 
       verify(spyTestObject , only()).someMethodNeedToBeChecked(); 
        ...
    }
}
like image 186
Jean-Philippe Bond Avatar answered Sep 27 '22 18:09

Jean-Philippe Bond