Continue after throwing an exception

I am throwing Exception into my unit test but after throwing it I still want to continue testing

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

      

Can this be done?

+3


source to share


1 answer


You can 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.

      

+9


source







All Articles