我在单元测试中抛出异常,但在抛出之后,我仍然希望能够继续测试
doThrow(new Exception()).when(myMock).myMethod();
myMock.myMethod();
System.out.println("Here"); // this is never called
// Do verify and asserts
是否可以这样做?
答案 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.