public class ExceptionAssertExtensions extends Object
The AssertThrow methods are necessary for a couple of reasons
@Test(exception=..)
paradigm does not work because it is only looking for at least one statement in a test
method to throw. If there are multiple statements in the test method that should throw, then they would have to be broken out
into individual test methods
try{statement; fail()}catch(Exception){assertTrue(true)}
paradigm works, but is unacceptably long and
redundant in the test code.
Exception
class also has the same drawbacks as the @Test
paradigm.
Modifier and Type | Method and Description |
---|---|
static void |
assertConstuctorThrows(Class<? extends Throwable> expectedException,
Constructor<?> constr,
Object... arguments)
assertThrows for Constructors using reflection
|
static void |
assertThrows(Class<? extends Throwable> expectedException,
Object target,
String methodName,
Object... arguments)
Unit test to assert that a specific type of exception is thrown
|
static <T extends Throwable> |
assertThrows(Class<T> excType,
Runnable throwerClosure)
Checks that the logic wrapped by the given Runnable throw an exception of the specified type (only valid for
RuntimeExceptions given that the run method of the Runnable interface exposes no throws signature).
|
static <T extends Throwable> |
assertThrows(Class<T> excType,
Runnable throwerClosure,
String customFailMessage)
Similar to the other assertThrows methods.
|
static void |
assertThrows(String message,
Class<? extends Throwable> expectedException,
Object target,
String methodName,
Object... arguments)
Unit test to assert that a specific type of exception with a specific message is thrown
|
static <T extends Throwable> |
assertThrows(String excMessage,
Class<T> excType,
Runnable throwerClosure)
Assert that a method in a
Runnable closure should throw when tested |
static <T extends Throwable> |
assertThrows(String excMessage,
Class<T> excType,
Runnable throwerClosure,
String customFailMessage)
Assert that a method in a
Runnable closure should throw when tested |
static <T extends Throwable> |
assertThrowsAndDoAssertsInCatch(Class<T> excType,
ExceptionAssertionsPerformer<T> excAssertsPerformer)
Checks if the given exception type is thrown and perform the given assertions in that exception object.
|
static <T extends Throwable> |
assertThrowsAndDoAssertsInCatch(Class<T> excType,
ExceptionAssertionsPerformer<T> excAssertsPerformer,
String customFailMessage)
Similar to the method with the same name.
|
static <T extends Throwable> |
assertThrowsSpecificException(String excMessage,
Class<T> excType,
ExceptionAssertionsPerformer<T> excAssertsPerformer,
String customFailMessage)
Assert that a method in a
Runnable closure should throw when tested with a specific message |
public static <T extends Throwable> void assertThrows(@NotNull Class<T> excType, @NotNull Runnable throwerClosure)
excType
- The Class corresponding to the expected exception.throwerClosure
- Closure-like object that represents the code expected to throw an exception.public static <T extends Throwable> void assertThrows(Class<T> excType, Runnable throwerClosure, String customFailMessage)
excType
- The Class corresponding to the expected exception.throwerClosure
- Closure-like object that represents the code expected to throw an exception.customFailMessage
- Message to throw if the wrong exception is thrownpublic static <T extends Throwable> void assertThrowsAndDoAssertsInCatch(Class<T> excType, ExceptionAssertionsPerformer<T> excAssertsPerformer)
excType
- The Class corresponding to the expected exception.excAssertsPerformer
- An object that provides methods to perform that will throw and methods to perform
after the catch.public static <T extends Throwable> void assertThrowsAndDoAssertsInCatch(Class<T> excType, ExceptionAssertionsPerformer<T> excAssertsPerformer, String customFailMessage)
excType
- The Class corresponding to the expected exception.excAssertsPerformer
- An object that provides methods to perform that will throw and methods to perform
after the catch.customFailMessage
- Message to throw if the wrong exception is thrownpublic static <T extends Throwable> void assertThrows(@NotNull String excMessage, @NotNull Class<T> excType, @NotNull Runnable throwerClosure)
Runnable
closure should throw when testedT
- A class that extends ThrowableexcMessage
- the expected message from the exceptionexcType
- The Class corresponding to the expected exception.throwerClosure
- Closure-like object that represents the code expected to throw an exception.public static <T extends Throwable> void assertThrows(@NotNull String excMessage, @NotNull Class<T> excType, @NotNull Runnable throwerClosure, String customFailMessage)
Runnable
closure should throw when testedT
- A class that extends ThrowableexcMessage
- the expected message from the exceptionexcType
- The Class corresponding to the expected exception.throwerClosure
- Closure-like object that represents the code expected to throw an exception.customFailMessage
- A message to be displayed on failurepublic static <T extends Throwable> void assertThrowsSpecificException(String excMessage, @NotNull Class<T> excType, @NotNull ExceptionAssertionsPerformer<T> excAssertsPerformer, String customFailMessage)
Runnable
closure should throw when tested with a specific messageT
- A class that extends ThrowableexcMessage
- the expected message from the exceptionexcType
- The Class corresponding to the expected exception.excAssertsPerformer
- An object that provides methods to perform that will throw and methods to perform
after the catch.customFailMessage
- A message to be displayed on failurepublic static void assertThrows(@NotNull Class<? extends Throwable> expectedException, @NotNull Object target, @NotNull String methodName, Object... arguments)
Examples:
assertThrows(NumberFormatException.class, new Double(0), "parseDouble", "a");
assertThrows(ArithmeticException.class, new Double(0), "parseDouble", "a");
assertThrows(NumberFormatException.class, new Double(0), "parseDouble", "1");
expectedException
- The class of the expected exception typetarget
- the target object that the method will be called frommethodName
- the name of the method that is to be calledarguments
- the arguments to be passed to the methodpublic static void assertThrows(@NotNull String message, @NotNull Class<? extends Throwable> expectedException, @NotNull Object target, @NotNull String methodName, Object... arguments)
message
- The expected messageexpectedException
- The class of the expected exception typetarget
- the target object that the method will be called frommethodName
- the name of the method that is to be calledarguments
- the arguments to be passed to the methodpublic static void assertConstuctorThrows(@NotNull Class<? extends Throwable> expectedException, @NotNull Constructor<?> constr, Object... arguments)
Warning: This method cannot tell the difference between constructors when the only difference is a primitive type. For example, it cannot tell the difference between A(double[], Object, double) and A(double[], Object, int)
expectedException
- The class of the expected exception typeconstr
- the target constructorarguments
- the arguments to be passed to the constructorCopyright © 2014. All rights reserved.