我在我的单元测试中抛出一个Exception,但是在抛出之后,我仍然希望能够继续测试
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.