public class CheckExpectedException extends Object
TestRule and allows to assert thrown exception in callback.
Usage in tests:
@Rule
public CheckExpectedException thrown = CheckExpectedException.none();
@Test
public void test_state_of_objects_raw_check() throws NotEnoughMoney {
coffeeMachine.insertCoin(2);
thrown.check(new Check() {
public void check(Throwable throwable) {
assertThat(coffeeMachine.getInsertedMoney()).isEqualTo(2); //assert state
}
});
coffeeMachine.getCoffee();
}
| Modifier | Constructor and Description |
|---|---|
protected |
CheckExpectedException() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
addCheck(Check check)
Add assertion callback to callback list.
|
Statement |
apply(Statement base,
Description description)
Method required by junit from rules implementations.
|
CheckExpectedException |
check(Check check)
Adds callback to assert thrown exception.
|
protected void |
failBecauseExceptionWasNotThrown()
Throws AssertionError with info that exception was expected but not thrown.
|
S |
handleAssertionErrors()
Starts handling of assertion errors.
|
S |
handleAssumptionViolatedExceptions()
Starts handling of AssumptionViolatedException.
|
protected void |
handleException(Throwable e)
Handles thrown exception.
|
boolean |
isExceptionExpected()
Returns if rule is expecting any exception.
|
protected S |
myself() |
static CheckExpectedException |
none()
Creates and returns new instance of
CheckExpectedException rule. |
public static CheckExpectedException none()
CheckExpectedException rule.public CheckExpectedException check(Check check)
check - callback to assert thrown exceptionprotected void addCheck(Check check)
check - callback to assert thrown exceptionpublic boolean isExceptionExpected()
true if any exception is expectedprotected void handleException(Throwable e)
e - thrown exceptionprotected void failBecauseExceptionWasNotThrown()
protected S myself()
public final S handleAssertionErrors()
this to allow chainingpublic final S handleAssumptionViolatedExceptions()
this to allow chainingpublic final Statement apply(Statement base, Description description)
Copyright © 2014. All rights reserved.