我在我的单元测试中抛出异常,但是抛出之后,我仍然希望能够继续测试

doThrow(new Exception()).when(myMock).myMethod();
myMock.myMethod();
System.out.println("Here"); // this is never called
// Do verify and asserts

是否有可能做到这一点?

最佳答案

您可以捕获异常:

doThrow(new MyException()).when(myMock).myMethod();

try {
    myMock.myMethod();
    fail("MyException should have been thrown!");
} catch (MyException expected) {
    // Do something
}

System.out.println("Here");
// Verify the mock, assert, etc.

关于java - 抛出异常后继续,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44272181/

10-10 06:56