public class CatchThrowable extends Object
| Constructor and Description |
|---|
CatchThrowable() |
| Modifier and Type | Method and Description |
|---|---|
static void |
catchThrowable(ThrowingCallable actor)
Use it to catch an throwable and to get access to the thrown throwable (for further verifications).
|
static void |
catchThrowable(ThrowingCallable actor,
Class<? extends Throwable> clazz)
Use it to catch an throwable of a specific type and to get access to the thrown throwable (for further
verifications).
|
private static void |
catchThrowable(ThrowingCallable actor,
Class<? extends Throwable> clazz,
boolean assertException) |
static <T extends Throwable> |
caughtThrowable()
Returns the throwable caught during the last call on the proxied object in the current thread.
|
static <T extends Throwable> |
caughtThrowable(Class<T> caughtThrowableType) |
static void |
resetCaughtThrowable()
Sets the
caught throwable to null. |
private static void |
validateArguments(ThrowingCallable actor,
Class<? extends Throwable> clazz) |
static void |
verifyThrowable(ThrowingCallable actor)
Use it to verify that an throwable is thrown and to get access to the thrown throwable (for further
verifications).
|
static void |
verifyThrowable(ThrowingCallable actor,
Class<? extends Throwable> clazz)
Use it to verify that an throwable of specific type is thrown and to get access to the thrown throwable (for
further verifications).
|
public CatchThrowable()
public static <T extends Throwable> T caughtThrowable()
T - throwable caught during the last call on the proxied objectverifyThrowable(ThrowingCallable, Class)
verifyThrowable()} or catchThrowable(ThrowingCallable). Returns null the proxy has
not caught an throwable. Returns null if the caught throwable belongs to a class that is no longer
loaded.public static <T extends Throwable> T caughtThrowable(Class<T> caughtThrowableType)
public static void verifyThrowable(ThrowingCallable actor)
verifyThrowable(obj).doX(); // catch and verify
assert "foobar".equals(caughtThrowable().getMessage()); // further analysis
If doX() does not throw a Throwable, then a ThrowableNotThrownAssertionError is
thrown. Otherwise the thrown throwable can be retrieved via caughtThrowable().actor - The instance that shall be proxied. Must not be null.public static void verifyThrowable(ThrowingCallable actor, Class<? extends Throwable> clazz)
verifyThrowable(obj, MyThrowable.class).doX(); // catch and verify
assert "foobar".equals(caughtThrowable().getMessage()); // further analysis
If doX() does not throw a MyThrowable, then a ThrowableNotThrownAssertionError
is thrown. Otherwise the thrown throwable can be retrieved via caughtThrowable().actor - The instance that shall be proxied. Must not be null.clazz - The type of the throwable that shall be thrown by the underlying object. Must not be
nullpublic static void catchThrowable(ThrowingCallable actor)
catchThrowable(obj).doX(); // catch
if (caughtThrowable() != null) {
assert "foobar".equals(caughtThrowable().getMessage()); // further analysis
} If doX()
throws a throwable, then caughtThrowable() will return the caught throwable. If doX() does
not throw a throwable, then caughtThrowable() will return null.actor - The instance that shall be proxied. Must not be null.public static void catchThrowable(ThrowingCallable actor, Class<? extends Throwable> clazz)
catchThrowable(obj, MyThrowable.class).doX(); // catch
if (caughtThrowable() != null) {
assert "foobar".equals(caughtThrowable().getMessage()); // further analysis
} If doX()
throws a MyThrowable, then caughtThrowable() will return the caught throwable. If
doX() does not throw a MyThrowable, then caughtThrowable() will return
null. If doX() throws an throwable of another type, i.e. not a subclass but another
class, then this throwable is not thrown and caughtThrowable() will return null.actor - The instance that shall be proxied. Must not be null.clazz - The type of the throwable that shall be caught. Must not be null.private static void catchThrowable(ThrowingCallable actor, Class<? extends Throwable> clazz, boolean assertException)
private static void validateArguments(ThrowingCallable actor, Class<? extends Throwable> clazz)
public static void resetCaughtThrowable()
caught throwable to null. This does not affect throwables saved at threads
other than the current one.
Actually you probably never need to call this method because each method call on a proxied object in the current
thread resets the caught throwable. But if you want to improve test isolation or if you want to 'clean up' after
testing (to avoid memory leaks), call the method before or after testing.Copyright © 2019. All rights reserved.