diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index fdf5d4f5b6f..67cae85f4c3 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -629,13 +629,12 @@ public abstract class ESTestCase extends LuceneTestCase { } /** Checks a specific exception class is thrown by the given runnable, and returns it. */ - @SuppressWarnings("unchecked") public static T expectThrows(Class expectedType, ThrowingRunnable runnable) { try { runnable.run(); } catch (Throwable e) { if (expectedType.isInstance(e)) { - return (T) e; + return expectedType.cast(e); } AssertionFailedError assertion = new AssertionFailedError("Unexpected exception type, expected " + expectedType.getSimpleName()); assertion.initCause(e);