Browse files

Actual exception added in ExpectedException

When using the ExpectedException to test for an exception to occur, it
throws an AssertionError when thrown exception does not match the
expected exception. Issue is however that in the JUnit runner results only
the actual exceptions message is shown and not the stacktrace. To fix
that, the actual exception is added to the cause of the AssertionError,
giving the exceptions in a format like:
java.lang.AssertionError: [reason]
  Expected: [expected exception]
     got: [actual exception + message]
 at [AssertionError stacktrace]
Caused by: [actual exception + message]
 at [actual exception stacktrace]
  • Loading branch information...
1 parent 45eaab7 commit 999546694db5005dc091c588433d91f5a7846f1d @arjenw committed Jan 3, 2012
Showing with 8 additions and 1 deletion.
  1. +8 −1 src/main/java/org/junit/rules/
@@ -111,7 +111,14 @@ public void evaluate() throws Throwable {
} catch (Throwable e) {
if (fMatcher == null)
throw e;
- Assert.assertThat(e, fMatcher);
+ try {
+ Assert.assertThat(e, fMatcher);
+ } catch (AssertionError ae) {
+ // add e as cause to have a complete error message in the
+ // results
+ ae.initCause(e);
+ throw ae;
+ }
if (fMatcher != null)

1 comment on commit 9995466

This needs a test case. I recommend that you roll back this commit, write the test case, verify that it fails, bring this code change back, and verify that it now passes.

Please sign in to comment.