mockito : how to unmock a method?

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

    Let's say most of your tests use the stubbed response. Then you would have a setUp() method that looks like this:

    @Before
    public void setUp() {
      wareHouseSpy = spy(realWarehouse);
      doReturn(false).when(wareHouseSpy).isSomethingMissing();
    }
    

    Now let's say you want to undo the stubbed response and use the real implementation in one test:

    @Test
    public void isSomethingMissing_useRealImplementation() {
      // Setup
      when(wareHouseSpy.isSomethingMissing()).thenCallRealMethod();
    
      // Test - Uses real implementation
      boolean result = wareHouseSpy.isSomethingMissing();
    }
    
    0 讨论(0)
提交回复
热议问题