views:

44

answers:

1

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 detection.

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");
}
A: 

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).

Péter Török
I can do it, but I just want to know if there is any other solution for that.
franziga
@franziga, what's your point - is it just theoretical interest, or do you want to actually achieve something concrete?
Péter Török
both :)Your workaround of course fulfills my requirement.
franziga