testFramework: (testing) Generify UsefulTestCase.assertThrows()

Make it throw T - the same exception type as the tested runnable block
declares.
This commit is contained in:
Eldar Abusalimov
2017-03-01 04:38:21 +03:00
parent 29c521c88a
commit 2e4b69cbdf
@@ -869,6 +869,7 @@ public abstract class UsefulTestCase extends TestCase {
* @param expectedErrorMsg expected error message
*/
protected void assertException(AbstractExceptionCase exceptionCase, @Nullable String expectedErrorMsg) throws Throwable {
//noinspection unchecked
assertExceptionOccurred(true, exceptionCase, expectedErrorMsg);
}
@@ -878,8 +879,8 @@ public abstract class UsefulTestCase extends TestCase {
* @param exceptionClass Expected exception type
* @param runnable Block annotated with some exception type
*/
protected void assertThrows(@NotNull Class<? extends Throwable> exceptionClass,
@NotNull ThrowableRunnable<?> runnable) throws Throwable {
protected <T extends Throwable> void assertThrows(@NotNull Class<? extends T> exceptionClass,
@NotNull ThrowableRunnable<T> runnable) throws T {
assertThrows(exceptionClass, null, runnable);
}
@@ -891,12 +892,13 @@ public abstract class UsefulTestCase extends TestCase {
* @param expectedErrorMsg expected error message, of any
* @param runnable Block annotated with some exception type
*/
protected void assertThrows(@NotNull Class<? extends Throwable> exceptionClass, @Nullable String expectedErrorMsg,
@NotNull ThrowableRunnable<?> runnable) throws Throwable {
assertException(new AbstractExceptionCase() {
@SuppressWarnings({"unchecked", "SameParameterValue"})
protected <T extends Throwable> void assertThrows(@NotNull Class<?> exceptionClass, @Nullable String expectedErrorMsg,
@NotNull ThrowableRunnable<T> runnable) throws T {
assertExceptionOccurred(true, new AbstractExceptionCase() {
@Override
public Class getExpectedExceptionClass() {
return exceptionClass;
public Class<Throwable> getExpectedExceptionClass() {
return (Class<Throwable>)exceptionClass;
}
@Override
@@ -911,7 +913,7 @@ public abstract class UsefulTestCase extends TestCase {
*
* @param exceptionCase Block annotated with some exception type
*/
protected void assertNoException(final AbstractExceptionCase exceptionCase) throws Throwable {
protected <T extends Throwable> void assertNoException(final AbstractExceptionCase<T> exceptionCase) throws T {
assertExceptionOccurred(false, exceptionCase, null);
}
@@ -926,9 +928,9 @@ public abstract class UsefulTestCase extends TestCase {
assertNull(throwableName);
}
private static void assertExceptionOccurred(boolean shouldOccur,
AbstractExceptionCase exceptionCase,
String expectedErrorMsg) throws Throwable {
private static <T extends Throwable> void assertExceptionOccurred(boolean shouldOccur,
AbstractExceptionCase<T> exceptionCase,
String expectedErrorMsg) throws T {
boolean wasThrown = false;
try {
exceptionCase.tryClosure();