public class ExpectedException extends java.lang.Object implements TestRule
// These tests all pass.
public static class HasExpectedException {
@Rule
public ExpectedException thrown= ExpectedException.none();
@Test
public void throwsNothing() {
// no exception expected, none thrown: passes.
}
@Test
public void throwsNullPointerException() {
thrown.expect(NullPointerException.class);
throw new NullPointerException();
}
@Test
public void throwsNullPointerExceptionWithMessage() {
thrown.expect(NullPointerException.class);
thrown.expectMessage("happened?");
thrown.expectMessage(startsWith("What"));
throw new NullPointerException("What happened?");
}
}
| Modifier and Type | Method and Description |
|---|---|
Statement |
apply(Statement base,
Description description)
Modifies the method-running
Statement to implement this
test-running rule. |
void |
expect(java.lang.Class<? extends java.lang.Throwable> type)
Adds to the list of requirements for any thrown exception that it
should be an instance of
type |
void |
expect(Matcher<?> matcher)
Adds
matcher to the list of requirements for any thrown exception. |
void |
expectMessage(Matcher<java.lang.String> matcher)
Adds
matcher to the list of requirements for the message
returned from any thrown exception. |
void |
expectMessage(java.lang.String substring)
Adds to the list of requirements for any thrown exception that it
should contain string
substring |
static ExpectedException |
none() |
public static ExpectedException none()
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 basebase,
a wrapper around base, or a completely new Statement.public void expect(Matcher<?> matcher)
matcher to the list of requirements for any thrown exception.public void expect(java.lang.Class<? extends java.lang.Throwable> type)
typepublic void expectMessage(java.lang.String substring)
substringpublic void expectMessage(Matcher<java.lang.String> matcher)
matcher to the list of requirements for the message
returned from any thrown exception.