| |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.junit.rules.ExpectedException
public class ExpectedException
The ExpectedException rule allows you to verify that your code throws a specific exception.
public class SimpleExpectedExceptionTest { @Rule public ExpectedException thrown= ExpectedException.none(); @Test public void throwsNothing() { // no exception expected, none thrown: passes. } @Test public void throwsExceptionWithSpecificType() { thrown.expect(NullPointerException.class); throw new NullPointerException(); } } You have to add the ExpectedException rule to your test. This doesn't affect your existing tests (see throwsNothing()). After specifiying the type of the expected exception your test is successful when such an exception is thrown and it fails if a different or no exception is thrown.
Instead of specifying the exception's type you can characterize the expected exception based on other criterias, too:
expectMessage(String)expectMessage(Matcher)expectCause(Matcher)expect(Matcher)You can combine any of the presented expect-methods. The test is successful if all specifications are met.
@Test public void throwsException() { thrown.expect(NullPointerException.class); thrown.expectMessage("happened"); throw new NullPointerException("What happened?"); } JUnit uses AssumptionViolatedExceptions for indicating that a test provides no useful information. (See Assume for more information.) You have to call assume methods before you set expectations of the ExpectedException rule. In this case the rule will not handle consume the exceptions and it can be handled by the framework. E.g. the following test is ignored by JUnit's default runner.
@Test public void ignoredBecauseOfFailedAssumption() { assumeTrue(false); // throws AssumptionViolatedException thrown.expect(NullPointerException.class); } JUnit uses AssertionErrors for indicating that a test is failing. You have to call assert methods before you set expectations of the ExpectedException rule, if they should be handled by the framework. E.g. the following test fails because of the assertTrue statement.
@Test public void throwsUnhandled() { assertTrue(false); // throws AssertionError thrown.expect(NullPointerException.class); } By default missing exceptions are reported with an error message like "Expected test to throw an instance of foo". You can configure a different message by means of reportMissingExceptionWithMessage(String). You can use a %s placeholder for the description of the expected exception. E.g. "Test doesn't throw %s." will fail with the error message "Test doesn't throw an instance of foo.".
| Method Summary | |
|---|---|
Statement | apply(Statement base, Description description) Modifies the method-running Statement to implement this test-running rule. |
void | expect(Class<? extends Throwable> type) Verify that your code throws an exception that is an instance of specific type. |
void | expect(Matcher<?> matcher) Verify that your code throws an exception that is matched by a Hamcrest matcher. |
void | expectCause(Matcher<? extends Throwable> expectedCause) Verify that your code throws an exception whose cause is matched by a Hamcrest matcher. |
void | expectMessage(Matcher<String> matcher) Verify that your code throws an exception whose message is matched by a Hamcrest matcher. |
void | expectMessage(String substring) Verify that your code throws an exception whose message contains a specific text. |
ExpectedException | handleAssertionErrors() Deprecated. AssertionErrors are handled by default since JUnit 4.12. Just like in JUnit <= 4.10. |
ExpectedException | handleAssumptionViolatedExceptions() Deprecated. AssumptionViolatedExceptions are handled by default since JUnit 4.12. Just like in JUnit <= 4.10. |
static ExpectedException | none() Returns a rule that expects no exception to be thrown (identical to behavior without this rule). |
ExpectedException | reportMissingExceptionWithMessage(String message) Specifies the failure message for tests that are expected to throw an exception but do not throw any. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static ExpectedException none()
@Deprecated public ExpectedException handleAssertionErrors()
@Deprecated public ExpectedException handleAssumptionViolatedExceptions()
public ExpectedException reportMissingExceptionWithMessage(String message)
%s placeholder for the description of the expected exception. E.g. "Test doesn't throw %s." will fail with the error message "Test doesn't throw an instance of foo.".
message - exception detail message public Statement apply(Statement base, Description description)
TestRuleStatement to implement this test-running rule.
apply in interface TestRulebase - The Statement to be modifieddescription - A Description of the test implemented in base base, a wrapper around base, or a completely new Statement.public void expect(Matcher<?> matcher)
@Test public void throwsExceptionThatCompliesWithMatcher() { NullPointerException e = new NullPointerException(); thrown.expect(is(e)); throw e; }
public void expect(Class<? extends Throwable> type)
type. @Test public void throwsExceptionWithSpecificType() { thrown.expect(NullPointerException.class); throw new NullPointerException(); }
public void expectMessage(String substring)
@Test public void throwsExceptionWhoseMessageContainsSpecificText() { thrown.expectMessage("happened"); throw new NullPointerException("What happened?"); }
public void expectMessage(Matcher<String> matcher)
@Test public void throwsExceptionWhoseMessageCompliesWithMatcher() { thrown.expectMessage(startsWith("What")); throw new NullPointerException("What happened?"); }
public void expectCause(Matcher<? extends Throwable> expectedCause)
@Test public void throwsExceptionWhoseCauseCompliesWithMatcher() { NullPointerException expectedCause = new NullPointerException(); thrown.expectCause(is(expectedCause)); throw new IllegalArgumentException("What happened?", cause); }
| |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||