Junit : add handling logic when exception is not expected
I am trying to print the stack trace of the exception.
However, for negative test case, only the unexpected exception is printed.
I am using the @Rule ExpectedException to do the exception detecti开发者_JAVA技巧on.
I don't know how to add handling logic in case an unexpected exception is thrown.
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void myTest() throws Exception {
thrown.expect(MyException.class);
thrown.expectMessage("expected message");
}
Can't you simply catch the exception within your test method, and then print the stack trace? (and then even rethrow it if you want).
Did you try implementing WatchmanTest.failed
?
Franziga, you might try catch-exception to satisfy your theoretical interest:
@Test
public void myTest() throws Exception {
catchException(obj).doSomethingExceptional();
log(caughtException());
assertTrue(caughtException() instanceof MyException.class);
assertEquals("expected message", caughtException().getMessage());
}
精彩评论