mockito : how to unmock a method?

后端 未结 7 1058
情深已故
情深已故 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: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
      ...
    }
    

提交回复
热议问题