mockito : how to unmock a method?

两盒软妹~` 提交于 2019-11-29 15:56:22

问题


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

I use Mockito to create a spy on real instance, and then override some method which is not relevant to the actual test I perform.

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?

say I have a spy object called 'wareHouseSpy'

say I overriden the method isSomethingMissing :

doReturn(false).when(wareHouseSpy).isSomethingMissing()

What will be the right way to un-override, and bring things back to normal on the spy i.e make the next invokation of isSomethingMissing to run the real method?

something like

doReturn(Mockito.RETURN_REAL_METHOD).when(wareHouseSpy).isSomethingSpy()

or maybe

Mockito.unmock(wareHouseSpy)

Who knows? I couldn't find nothing in that area

Thanks!

Assaf


回答1:


I think

Mockito.reset(wareHouseSpy)

would do it.




回答2:


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();
}



回答3:


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



回答4:


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

Object mySpy = spy(real);

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




回答5:


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
  ...
}



回答6:


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.



来源:https://stackoverflow.com/questions/8123091/mockito-how-to-unmock-a-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!