mockito : how to unmock a method?

后端 未结 7 1008
情深已故
情深已故 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:10

    Maybe I am not following but when you have a real object real:

    Object mySpy = spy(real);
    

    Then to "unspy" mySpy... just use real.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-01 09:14

    The "normal" way is to re-instantiate things in your "setUp" method. However, if you have a real object that is expensive to construct for some reason, you could do something like this:

    public class MyTests {
    
      private static MyBigWarehouse realWarehouse = new MyBigWarehouse();
      private MyBigWarehouse warehouseSpy;
    
      @Before
      public void setUp() {
        warehouseSpy = spy(realWarehouse); // same real object - brand new spy!
        doReturn(false).when(wareHouseSpy).isSomethingMissing();
      }
    
      @Test
      ...
    
      @Test
      ...
    
      @Test
      ...
    }
    
    0 讨论(0)
  • 2021-01-01 09:20

    Addressing this piece specifically:

    Is there a way, just for the sake of cleaning up after me in case some other tests that run after my tests also use the same instances and might execute a mocked method they didn't ask to mock, to un-mock a method?

    If you are using JUnit, the cleanest way to do this is to use @Before and @After (other frameworks have equivalents) and recreate the instance and the spy so that no test depends on or is impacted by whatever you have done on any other test. Then you can do the test-specific configuration of the spy/mock inside of each test. If for some reason you don't want to recreate the object, you can recreate the spy. Either way, everyone starts with a fresh spy each time.

    0 讨论(0)
  • 2021-01-01 09:22

    I think

    Mockito.reset(wareHouseSpy)
    

    would do it.

    0 讨论(0)
  • 2021-01-01 09:25

    It depends whether you are testing with TestNG or JUnit.

    • JUnit creates a new instance of itself for each test method. You basically don't have to worry about reseting mocks.
    • With TestNG, you have to reset the mock(s) with Mockito.reset(mockA, mockB, ...) in either an @BeforeMethod or an @AfterMethod
    0 讨论(0)
提交回复
热议问题