public class CatchThrowable extends Object
Constructor and Description |
---|
CatchThrowable() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
catchThrowable(T obj)
Use it to catch an throwable and to get access to the thrown throwable (for further verifications).
|
static <T,E extends Throwable> |
catchThrowable(T obj,
Class<E> clazz)
Use it to catch an throwable of a specific type and to get access to the thrown throwable (for further
verifications).
|
static <E extends Throwable> |
caughtThrowable()
Returns the throwable caught during the last call on the proxied object in the current thread.
|
static <T> T |
interfaces(T obj)
Returns a proxy that implements all interfaces of the underlying object.
|
private static <T,E extends Throwable> |
processThrowable(T obj,
Class<E> throwableClazz,
boolean assertThrowable)
Creates a proxy that processes throwables thrown by the underlying object.
|
static void |
resetCaughtThrowable()
Sets the
caught throwable to null. |
static <T> T |
verifyThrowable(T obj)
Use it to verify that an throwable is thrown and to get access to the thrown throwable (for further
verifications).
|
static <T,E extends Throwable> |
verifyThrowable(T obj,
Class<E> 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 <E extends Throwable> E caughtThrowable()
E
- This type parameter makes some type casts redundant.verifyThrowable()
or catchThrowable()
. 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> T verifyThrowable(T obj)
The following example verifies that obj.doX() throws a Throwable:
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()
.
T
- The type of the given obj
.obj
- The instance that shall be proxied. Must not be null
.public static <T,E extends Throwable> T verifyThrowable(T obj, Class<E> clazz)
The following example verifies that obj.doX() throws a MyThrowable:
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()
.
T
- The type of the given obj
.E
- The type of the throwable that shall be caught.obj
- 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 null
.public static <T> T catchThrowable(T obj)
In the following example you catch throwables that are thrown by obj.doX():
If catchThrowable(obj).doX(); // catch
if (caughtThrowable() != null) {
assert "foobar".equals(caughtThrowable().getMessage()); // further analysis
}
doX()
throws a throwable, then caughtThrowable()
will return the caught throwable. If doX()
does
not throw a throwable, then caughtThrowable()
will return null
.
T
- The type of the given obj
.obj
- The instance that shall be proxied. Must not be null
.public static <T,E extends Throwable> T catchThrowable(T obj, Class<E> clazz)
In the following example you catch throwables of type MyThrowable that are thrown by obj.doX():
If catchThrowable(obj, MyThrowable.class).doX(); // catch
if (caughtThrowable() != null) {
assert "foobar".equals(caughtThrowable().getMessage()); // further analysis
}
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
.
T
- The type of the given obj
.E
- The type of the throwable that shall be caught.obj
- 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 <T,E extends Throwable> T processThrowable(T obj, Class<E> throwableClazz, boolean assertThrowable)
Delegates to SubclassProxyFactory.createProxy(Class, MethodInterceptor)
which itself might delegate to
InterfaceOnlyProxyFactory.createProxy(Class, MethodInterceptor)
.
public static <T> T interfaces(T obj)
T
- must be an interface the object implementsobj
- the object that created proxy will delegate all calls topublic 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 © 2014. All rights reserved.