抛出异常后继续

时间:2017-05-30 21:26:10

标签: java exception mockito

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

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

是否可以这样做?

1 个答案:

答案 0 :(得分:9)

你可以抓住异常:

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.