Skip to content

Instantly share code, notes, and snippets.

@brunokim
Last active September 21, 2016 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunokim/9675101 to your computer and use it in GitHub Desktop.
Save brunokim/9675101 to your computer and use it in GitHub Desktop.
Example of stacktrace hiding with ExpectedException rule
// I'm not sure this is the minimal possible example, edits are welcome
package assertion;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class ExpectedExceptionTest {
@Rule public final ExpectedException thrown = ExpectedException.none();
@Test public void shouldFailWithStackTrace() {
try {
throw new NullPointerException();
} finally {
Assert.assertNull(new Object());
}
}
@Test public void shouldFailWithoutStackTrace() {
thrown.expect(IllegalArgumentException.class); // Expectation not satisfied
try {
throw new NullPointerException();
} finally {
Assert.assertNull(new Object());
}
}
}
@sarnobat
Copy link

The stack trace still gets printed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment