PowerMock: How to unmock a method?

前端 未结 2 1254
闹比i
闹比i 2021-02-19 02:27

I have a static method that is mocked using PowerMock to throw an exception. (It deletes files.) Unfortunately, during my @After (after-each-test) method, I need

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-19 03:01

    This took me a while to figure out, so I am answering my own question.

    AFAIK, you need to "undo" each mock. Mockito.reset() will not work with Class references. At the end of the test method, add:

    // Undo the mock above because we need to call PathUtils.removeFile() within @After.
    PowerMockito.doCallRealMethod().when(PathUtils.class);
    PathUtils.removeFile(Mockito.any(File.class));
    

提交回复
热议问题