diff --git a/src/main/java/org/junit/Assert.java b/src/main/java/org/junit/Assert.java index d89175570b38..6b4d072a3b19 100755 --- a/src/main/java/org/junit/Assert.java +++ b/src/main/java/org/junit/Assert.java @@ -968,50 +968,51 @@ public interface ThrowingRunnable { } /** - * Asserts that {@code runnable} throws an exception of type {@code throwableClass} when + * Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when * executed. If it does not throw an exception, an {@link AssertionError} is thrown. If it * throws the wrong type of exception, an {@code AssertionError} is thrown describing the * mismatch; the exception that was actually thrown can be obtained by calling {@link * AssertionError#getCause}. * - * @param throwableClass the expected type of the exception + * @param expectedThrowable the expected type of the exception * @param runnable a function that is expected to throw an exception when executed * @since 4.13 */ - public static void assertThrows(Class throwableClass, ThrowingRunnable runnable) { - expectThrows(throwableClass, runnable); + public static void assertThrows(Class expectedThrowable, ThrowingRunnable runnable) { + expectThrows(expectedThrowable, runnable); } /** - * Asserts that {@code runnable} throws an exception of type {@code throwableClass} when + * Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when * executed. If it does, the exception object is returned. If it does not throw an exception, an * {@link AssertionError} is thrown. If it throws the wrong type of exception, an {@code * AssertionError} is thrown describing the mismatch; the exception that was actually thrown can * be obtained by calling {@link AssertionError#getCause}. * - * @param throwableClass the expected type of the exception + * @param expectedThrowable the expected type of the exception * @param runnable a function that is expected to throw an exception when executed * @return the exception thrown by {@code runnable} * @since 4.13 */ - public static T expectThrows(Class throwableClass, ThrowingRunnable runnable) { + public static T expectThrows(Class expectedThrowable, ThrowingRunnable runnable) { try { runnable.run(); - } catch (Throwable t) { - if (throwableClass.isInstance(t)) { - return throwableClass.cast(t); + } catch (Throwable actualThrown) { + if (expectedThrowable.isInstance(actualThrown)) { + @SuppressWarnings("unchecked") T retVal = (T) actualThrown; + return retVal; } else { - String mismatchMessage = String.format("Expected %s to be thrown, but %s was thrown", - throwableClass.getSimpleName(), t.getClass().getSimpleName()); + String mismatchMessage = format("unexpected exception type thrown;", + expectedThrowable.getSimpleName(), actualThrown.getClass().getSimpleName()); // The AssertionError(String, Throwable) ctor is only available on JDK7. AssertionError assertionError = new AssertionError(mismatchMessage); - assertionError.initCause(t); + assertionError.initCause(actualThrown); throw assertionError; } } - String message = String.format("Expected %s to be thrown, but nothing was thrown", - throwableClass.getSimpleName()); + String message = String.format("expected %s to be thrown, but nothing was thrown", + expectedThrowable.getSimpleName()); throw new AssertionError(message); } } diff --git a/src/test/java/org/junit/tests/assertion/AssertionTest.java b/src/test/java/org/junit/tests/assertion/AssertionTest.java index ae198ff8ad5a..fab5e805ca66 100755 --- a/src/test/java/org/junit/tests/assertion/AssertionTest.java +++ b/src/test/java/org/junit/tests/assertion/AssertionTest.java @@ -688,7 +688,7 @@ public void expectThrowsIncludesAnInformativeDefaultMessage() { try { expectThrows(Throwable.class, nonThrowingRunnable()); } catch (AssertionError ex) { - assertEquals("Expected Throwable to be thrown, but nothing was thrown", ex.getMessage()); + assertEquals("expected Throwable to be thrown, but nothing was thrown", ex.getMessage()); return; } fail(); @@ -731,7 +731,7 @@ public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() { try { expectThrows(IOException.class, throwingRunnable(npe)); } catch (AssertionError error) { - assertEquals("Expected IOException to be thrown, but NullPointerException was thrown", + assertEquals("unexpected exception type thrown; expected: but was:", error.getMessage()); assertSame(npe, error.getCause()); return;