public final class ThrowableTester extends Object
This class does not offer a message parameter to display when the test fails. The purpose of the test should be clear by the test name, the utilities in this class deals with expected throwables and proper error messages will be displayed if the assertion fails.
Note: Throwable isn't generally caught. Throwable is the superclass to Exception and Error. Errors are generally things which a normal application wouldn't and shouldn't catch, so just use Exception unless you have a specific reason to use Throwable. In this case ThrowableTester checks whether an expected Throwable is actually thrown.
Note that JUnit already offers a way to assert expected exceptions, however for complex setups the exception may come from an unexpected source hiding a problem. Example:
// Good usage of the JUnit annotation
@Test(expected = NullPointerException.class)
public void test() {
ClassUnderTest testable = ...;
testable.methodExpectingToThrowException();
}
// Dangerous usage of the JUnit annotation
@Test(expected = NullPointerException.class)
public void test() {
setUpOperation_1;
... // If any of the setup operations throw a NullPointerException the test will pass hiding a problem
setUpOperation_n;
ClassUnderTest testable = ...;
testable.methodExpectingToThrowException();
}
// Alternative using ThrowableTester
@Test
public void test() {
setUpOperation_1;
... // If any of the setup operations throw a NullPointerException the test will fail uncovering a problem
setUpOperation_n;
final ClassUnderTest testable = ...;
ThrowableTester.testThrows(NullPointerException.class, new Instruction(){
@Override
public void execute() throws Throwable {
testable.methodExpectingToThrowException();
}
});
}
| Modifier and Type | Class and Description |
|---|---|
static interface |
ThrowableTester.Instruction
Instruction.
|
static interface |
ThrowableTester.Validator<E extends Throwable>
Throwable validator.
|
| Modifier and Type | Method and Description |
|---|---|
static <T extends Throwable> |
assertAnyThrowableType(Class<T> expected,
Throwable actual)
Asserts that an exception is of (or a subclass of) the given expected type.
|
static <T extends Throwable> |
assertThrowableType(Class<T> expected,
Throwable actual)
Asserts that an exception is exactly of the given expected type.
|
static <T extends Throwable> |
testThrows(Class<T> expectedThrowable,
ThrowableTester.Instruction instruction)
Executes an instruction verifying a
Throwable of the expected type is actually
thrown. |
static <T extends Throwable> |
testThrows(Class<T> expectedThrowable,
ThrowableTester.Instruction instruction,
ThrowableTester.Validator<T> throwableValidator)
Executes an instruction verifying a
Throwable of the expected type is actually
thrown. |
static <T extends Throwable> |
testThrowsAny(Class<T> expectedThrowable,
ThrowableTester.Instruction instruction)
Executes an instruction verifying a
Throwable of (or a subclass of) the expected type
is actually thrown. |
static <T extends Throwable> |
testThrowsAny(Class<T> expectedThrowable,
ThrowableTester.Instruction instruction,
ThrowableTester.Validator<T> throwableValidator)
Executes an instruction verifying a
Throwable of (or a subclass of) the expected type
is actually thrown. |
public static <T extends Throwable> void assertAnyThrowableType(Class<T> expected, Throwable actual)
public static <T extends Throwable> void assertThrowableType(Class<T> expected, Throwable actual)
public static <T extends Throwable> void testThrowsAny(Class<T> expectedThrowable, ThrowableTester.Instruction instruction)
Throwable of (or a subclass of) the expected type
is actually thrown.expectedThrowable - expected Throwableinstruction - instruction to executepublic static <T extends Throwable> void testThrowsAny(Class<T> expectedThrowable, ThrowableTester.Instruction instruction, ThrowableTester.Validator<T> throwableValidator)
Throwable of (or a subclass of) the expected type
is actually thrown.expectedThrowable - expected Throwableinstruction - instruction to executethrowableValidator - validator is used to do additional inspection to the errorpublic static <T extends Throwable> void testThrows(Class<T> expectedThrowable, ThrowableTester.Instruction instruction)
Throwable of the expected type is actually
thrown.expectedThrowable - expected Throwableinstruction - instruction to executepublic static <T extends Throwable> void testThrows(Class<T> expectedThrowable, ThrowableTester.Instruction instruction, ThrowableTester.Validator<T> throwableValidator)
Throwable of the expected type is actually
thrown.expectedThrowable - expected Throwableinstruction - instruction to executethrowableValidator - validator is used to do additional inspection to the errorCopyright © 2015. All rights reserved.