|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.junit.rules.ExpectedException
public class 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?"); } }
Method Summary | |
---|---|
Statement |
apply(Statement base,
FrameworkMethod method,
Object target)
Modifies the method-running Statement to implement an additional
test-running rule. |
void |
expect(Class<? extends 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<String> matcher)
Adds matcher to the list of requirements for the message
returned from any thrown exception. |
void |
expectMessage(String substring)
Adds to the list of requirements for any thrown exception that it should contain string substring |
static ExpectedException |
none()
|
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static ExpectedException none()
public Statement apply(Statement base, FrameworkMethod method, Object target)
MethodRule
Statement
to implement an additional
test-running rule.
apply
in interface MethodRule
base
- The Statement
to be modifiedmethod
- The method to be runtarget
- The object on with the method will be run.
base
,
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(Class<? extends Throwable> type)
type
public void expectMessage(String substring)
substring
public void expectMessage(Matcher<String> matcher)
matcher
to the list of requirements for the message
returned from any thrown exception.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |