Class CatchThrowable
java.lang.Object
com.googlecode.catchexception.throwable.CatchThrowable
The Class CatchThrowable.
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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).static <T extends Throwable>
TReturns the throwable caught during the last call on the proxied object in the current thread.static <T extends Throwable>
TcaughtThrowable
(Class<T> caughtThrowableType) Deprecated, for removal: This API element is subject to removal in a future version.Use caghtThrowable() instead as the passed argument does nothingstatic void
Sets thecaught throwable
to null.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).
-
Method Details
-
caughtThrowable
Returns the throwable caught during the last call on the proxied object in the current thread.- Type Parameters:
T
- throwable caught during the last call on the proxied object- Returns:
- Returns the throwable caught during the last call on the proxied object in the current thread - if the
call was made through a proxy that has been created via
verifyThrowable(ThrowingCallable, Class)
verifyThrowable()} orcatchThrowable(ThrowingCallable)
. Returns null the proxy has not caught an throwable. Returns null if the caught throwable belongs to a class that is no longerloaded
.
-
caughtThrowable
@Deprecated(since="2.3.0", forRemoval=true) public static <T extends Throwable> T caughtThrowable(Class<T> caughtThrowableType) Deprecated, for removal: This API element is subject to removal in a future version.Use caghtThrowable() instead as the passed argument does nothingCaught throwable.- Type Parameters:
T
- the generic type- Parameters:
caughtThrowableType
- the caught throwable type- Returns:
- the t
-
verifyThrowable
Use it to verify that an throwable is thrown and to get access to the thrown throwable (for further verifications). The following example verifies that obj.doX() throws a Throwable:verifyThrowable(obj).doX(); // catch and verify assert "foobar".equals(caughtThrowable().getMessage()); // further analysis
IfdoX()
does not throw aThrowable
, then aThrowableNotThrownAssertionError
is thrown. Otherwise the thrown throwable can be retrieved viacaughtThrowable()
.- Parameters:
actor
- The instance that shall be proxied. Must not benull
.
-
verifyThrowable
Use it to verify that an throwable of specific type is thrown and to get access to the thrown throwable (for further verifications). 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
IfdoX()
does not throw aMyThrowable
, then aThrowableNotThrownAssertionError
is thrown. Otherwise the thrown throwable can be retrieved viacaughtThrowable()
.- Parameters:
actor
- The instance that shall be proxied. Must not benull
.clazz
- The type of the throwable that shall be thrown by the underlying object. Must not benull
-
catchThrowable
Use it to catch an throwable and to get access to the thrown throwable (for further verifications). In the following example you catch throwables that are thrown by obj.doX():catchThrowable(obj).doX(); // catch if (caughtThrowable() != null) { assert "foobar".equals(caughtThrowable().getMessage()); // further analysis }
IfdoX()
throws a throwable, thencaughtThrowable()
will return the caught throwable. IfdoX()
does not throw a throwable, thencaughtThrowable()
will returnnull
.- Parameters:
actor
- The instance that shall be proxied. Must not benull
.
-
catchThrowable
Use it to catch an throwable of a specific type and to get access to the thrown throwable (for further verifications). In the following example you catch throwables of type MyThrowable that are thrown by obj.doX():catchThrowable(obj, MyThrowable.class).doX(); // catch if (caughtThrowable() != null) { assert "foobar".equals(caughtThrowable().getMessage()); // further analysis }
IfdoX()
throws aMyThrowable
, thencaughtThrowable()
will return the caught throwable. IfdoX()
does not throw aMyThrowable
, thencaughtThrowable()
will returnnull
. IfdoX()
throws an throwable of another type, i.e. not a subclass but another class, then this throwable is not thrown andcaughtThrowable()
will returnnull
.- Parameters:
actor
- The instance that shall be proxied. Must not benull
.clazz
- The type of the throwable that shall be caught. Must not benull
.
-
resetCaughtThrowable
public static void resetCaughtThrowable()Sets thecaught 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.
-