Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continue after throwing an exception

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?

like image 876
Spark323 Avatar asked Feb 15 '26 09:02

Spark323


1 Answers

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.
like image 176
Mureinik Avatar answered Feb 16 '26 21:02

Mureinik