I'm throwing an Exception in my unit test, but after it is thrown, I still want to be able to continue testing
doThrow(new Exception()).when(myMock).myMethod();
myMock.myMethod();
System.out.println("Here"); // this is never called
// Do verify and asserts
Is it possible to do this?
You could just catch the exception:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With