mockito : how to unmock a method?

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

提交回复
热议问题