mockito : how to unmock a method?

后端 未结 7 1010
情深已故
情深已故 2021-01-01 08:48

I have a JUnit class with different methods to perform different tests.

I use Mockito to create a spy on real instance, and then ov

7条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-01 09:13

    As per the documentation, we have

    reset(mock);
    //at this point the mock forgot any interactions & stubbing
    

    The documentation specifies further

    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.

    Here's an example from their github repo which tests this behavior and uses it:

    @Test
    public void shouldRemoveAllInteractions() throws Exception {
            mock.simpleMethod(1);
            reset(mock);
            verifyZeroInteractions(mock);
    }
    

    reference : ResetTest.java

提交回复
热议问题