Expect provides a mechanism to alter the success/fail behaviour
of a unit test without modifying the code.
Expect handles the following 3 scenarios:
- Successful completion of a unit test.
- Failure of a unit test with an exception.
- Failure of a unit test by a failure to throw an exception
The setup of unit tests have the following 2 patterns:
1)
@Expect({Condition1, Condition2, ....})
public void testExample1() {
try {
...
handleSuccess();
} catch(Exception e) {
handleException(e);
}
}
2)
@Expect({Condition1, Condition2, ....})
public void testExample2() {
try {
...
try {
... // this should throw
handleFailure();
} catch(SpecificException e) {
handleSuccess();
}
} catch(Exception e) {
handleException(e);
}
}
The arguments to Expect are an array of strings.
handleSuccess() verifies that there was no exception that should have been thrown.
handleFailure() verifies that no exception was correct for the configuration.
handleException() verifies that the exception thrown was the correct one for the configuration.
For a unit test that always throws, the following pattern should be used: