java.lang.Object | |
↳ | org.junit.rules.ExpectedException |
The ExpectedException Rule allows in-test specification of expected exception types and messages:
// These tests all pass. public static class HasExpectedException { @Rule public ExpectedException thrown= new ExpectedException(); @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?"); } }
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Modifies the method-running
Statement to implement an additional
test-running rule. | |||||||||||
Adds to the list of requirements for any thrown exception that it
should be an instance of
type
| |||||||||||
Adds
matcher to the list of requirements for any thrown exception. | |||||||||||
Adds to the list of requirements for any thrown exception that it
should contain string
substring
| |||||||||||
Adds
matcher to the list of requirements for the message
returned from any thrown exception. | |||||||||||
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
org.junit.rules.MethodRule
|
Modifies the method-running Statement
to implement an additional
test-running rule.
base | The Statement to be modified |
---|---|
method | The method to be run |
target | The object on with the method will be run. |
base
,
a wrapper around base
, or a completely new Statement.
Adds to the list of requirements for any thrown exception that it
should be an instance of type
Adds matcher
to the list of requirements for any thrown exception.
Adds to the list of requirements for any thrown exception that it
should contain string substring
Adds matcher
to the list of requirements for the message
returned from any thrown exception.