org.hamcrest.junit
public class ExpectedException extends java.lang.Object implements org.junit.rules.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?");
}
@Test
public void throwsIllegalArgumentExceptionWithMessageAndCause() {
NullPointerException expectedCause = new NullPointerException();
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("What");
thrown.expectCause(is(expectedCause));
throw new IllegalArgumentException("What happened?", cause);
}
}
By default ExpectedException rule doesn't handle AssertionErrors and
AssumptionViolatedExceptions, because such exceptions are used by JUnit. If
you want to handle such exceptions you have to call @link
handleAssertionErrors() or @link
handleAssumptionViolatedExceptions().
// These tests all pass.
public static class HasExpectedException {
@Rule
public ExpectedException thrown= ExpectedException.none();
@Test
public void throwExpectedAssertionError() {
thrown.handleAssertionErrors();
thrown.expect(AssertionError.class);
throw new AssertionError();
}
@Test
public void throwExpectAssumptionViolatedException() {
thrown.handleAssumptionViolatedExceptions();
thrown.expect(AssumptionViolatedException.class);
throw new AssumptionViolatedException("");
}
}
| Modifier and Type | Method and Description |
|---|---|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description) |
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(org.hamcrest.Matcher<?> matcher)
Adds
matcher to the list of requirements for any thrown
exception. |
void |
expectCause(org.hamcrest.Matcher<? super java.lang.Throwable> expectedCause)
Adds
matcher to the list of requirements for the cause of
any thrown exception. |
void |
expectMessage(org.hamcrest.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 |
ExpectedException |
handleAssertionErrors() |
ExpectedException |
handleAssumptionViolatedExceptions() |
static ExpectedException |
none() |
public static ExpectedException none()
public ExpectedException handleAssertionErrors()
public ExpectedException handleAssumptionViolatedExceptions()
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description)
apply in interface org.junit.rules.TestRulepublic void expect(org.hamcrest.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(org.hamcrest.Matcher<java.lang.String> matcher)
matcher to the list of requirements for the message returned
from any thrown exception.public void expectCause(org.hamcrest.Matcher<? super java.lang.Throwable> expectedCause)
matcher to the list of requirements for the cause of
any thrown exception.