From 02a78008a8fe500f7836015b0743bc3947ddb5c4 Mon Sep 17 00:00:00 2001 From: Kevin Cooney Date: Wed, 30 Nov 2016 09:22:46 -0800 Subject: [PATCH 1/2] Delete expectThrows(). Change assertThrows() to return the caught exception. JUnit 5 decided to make the same change in the Assertions class, so this change makes the JUnit4 API consistent. --- src/main/java/org/junit/Assert.java | 17 +-------- .../org/junit/function/ThrowingRunnable.java | 4 +- .../org/junit/rules/ExpectedException.java | 5 +-- .../junit/tests/assertion/AssertionTest.java | 38 +++++++++---------- 4 files changed, 23 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/junit/Assert.java b/src/main/java/org/junit/Assert.java index a2ca44edbf1c..bb2c4bc93661 100755 --- a/src/main/java/org/junit/Assert.java +++ b/src/main/java/org/junit/Assert.java @@ -966,21 +966,6 @@ public static void assertThat(String reason, T actual, MatcherAssert.assertThat(reason, actual, matcher); } - /** - * 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 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 expectedThrowable, ThrowingRunnable runnable) { - expectThrows(expectedThrowable, runnable); - } - /** * 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 @@ -993,7 +978,7 @@ public static void assertThrows(Class expectedThrowable, Th * @return the exception thrown by {@code runnable} * @since 4.13 */ - public static T expectThrows(Class expectedThrowable, ThrowingRunnable runnable) { + public static T assertThrows(Class expectedThrowable, ThrowingRunnable runnable) { try { runnable.run(); } catch (Throwable actualThrown) { diff --git a/src/main/java/org/junit/function/ThrowingRunnable.java b/src/main/java/org/junit/function/ThrowingRunnable.java index 6e05d85c6580..e8b095a5308b 100644 --- a/src/main/java/org/junit/function/ThrowingRunnable.java +++ b/src/main/java/org/junit/function/ThrowingRunnable.java @@ -1,8 +1,8 @@ package org.junit.function; /** - * This interface facilitates the use of expectThrows from Java 8. It allows method references - * to void methods (that declare checked exceptions) to be passed directly into expectThrows + * This interface facilitates the use of assertThrows from Java 8. It allows method references + * to void methods (that declare checked exceptions) to be passed directly into assertThrows * without wrapping. It is not meant to be implemented directly. * * @since 4.13 diff --git a/src/main/java/org/junit/rules/ExpectedException.java b/src/main/java/org/junit/rules/ExpectedException.java index 0f1cb82bd309..4457dab264fc 100644 --- a/src/main/java/org/junit/rules/ExpectedException.java +++ b/src/main/java/org/junit/rules/ExpectedException.java @@ -18,10 +18,7 @@ * {@link org.junit.Assert#assertThrows(java.lang.Class, org.junit.function.ThrowingRunnable) * Assert.assertThrows} * is often a better choice since it allows you to express exactly where you - * expect the exception to be thrown. Use - * {@link org.junit.Assert#expectThrows(java.lang.Class, - * org.junit.function.ThrowingRunnable) expectThrows} - * if you need to assert something about the thrown exception. + * expect the exception to be thrown. * *

Usage

* diff --git a/src/test/java/org/junit/tests/assertion/AssertionTest.java b/src/test/java/org/junit/tests/assertion/AssertionTest.java index 0c5746a6e817..ced5e00df8df 100755 --- a/src/test/java/org/junit/tests/assertion/AssertionTest.java +++ b/src/test/java/org/junit/tests/assertion/AssertionTest.java @@ -10,8 +10,8 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.expectThrows; import static org.junit.Assert.fail; import java.io.IOException; @@ -797,14 +797,14 @@ public void assertNotEqualsIgnoresFloatDeltaOnNaN() { } @Test(expected = AssertionError.class) - public void expectThrowsRequiresAnExceptionToBeThrown() { - expectThrows(Throwable.class, nonThrowingRunnable()); + public void assertThrowsRequiresAnExceptionToBeThrown() { + assertThrows(Throwable.class, nonThrowingRunnable()); } @Test - public void expectThrowsIncludesAnInformativeDefaultMessage() { + public void assertThrowsIncludesAnInformativeDefaultMessage() { try { - expectThrows(Throwable.class, nonThrowingRunnable()); + assertThrows(Throwable.class, nonThrowingRunnable()); } catch (AssertionError ex) { assertEquals("expected java.lang.Throwable to be thrown, but nothing was thrown", ex.getMessage()); return; @@ -813,27 +813,27 @@ public void expectThrowsIncludesAnInformativeDefaultMessage() { } @Test - public void expectThrowsReturnsTheSameObjectThrown() { + public void assertThrowsReturnsTheSameObjectThrown() { NullPointerException npe = new NullPointerException(); - Throwable throwable = expectThrows(Throwable.class, throwingRunnable(npe)); + Throwable throwable = assertThrows(Throwable.class, throwingRunnable(npe)); assertSame(npe, throwable); } @Test(expected = AssertionError.class) - public void expectThrowsDetectsTypeMismatchesViaExplicitTypeHint() { + public void assertThrowsDetectsTypeMismatchesViaExplicitTypeHint() { NullPointerException npe = new NullPointerException(); - expectThrows(IOException.class, throwingRunnable(npe)); + assertThrows(IOException.class, throwingRunnable(npe)); } @Test - public void expectThrowsWrapsAndPropagatesUnexpectedExceptions() { + public void assertThrowsWrapsAndPropagatesUnexpectedExceptions() { NullPointerException npe = new NullPointerException("inner-message"); try { - expectThrows(IOException.class, throwingRunnable(npe)); + assertThrows(IOException.class, throwingRunnable(npe)); } catch (AssertionError ex) { assertSame(npe, ex.getCause()); assertEquals("inner-message", ex.getCause().getMessage()); @@ -843,11 +843,11 @@ public void expectThrowsWrapsAndPropagatesUnexpectedExceptions() { } @Test - public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() { + public void assertThrowsSuppliesACoherentErrorMessageUponTypeMismatch() { NullPointerException npe = new NullPointerException(); try { - expectThrows(IOException.class, throwingRunnable(npe)); + assertThrows(IOException.class, throwingRunnable(npe)); } catch (AssertionError error) { assertEquals("unexpected exception type thrown; expected: but was:", error.getMessage()); @@ -858,11 +858,11 @@ public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() { } @Test - public void expectThrowsUsesCanonicalNameUponTypeMismatch() { + public void assertThrowsUsesCanonicalNameUponTypeMismatch() { NullPointerException npe = new NullPointerException(); try { - expectThrows(NestedException.class, throwingRunnable(npe)); + assertThrows(NestedException.class, throwingRunnable(npe)); } catch (AssertionError error) { assertEquals( "unexpected exception type thrown; expected:" @@ -875,12 +875,12 @@ public void expectThrowsUsesCanonicalNameUponTypeMismatch() { } @Test - public void expectThrowsUsesNameUponTypeMismatchWithAnonymousClass() { + public void assertThrowsUsesNameUponTypeMismatchWithAnonymousClass() { NullPointerException npe = new NullPointerException() { }; try { - expectThrows(IOException.class, throwingRunnable(npe)); + assertThrows(IOException.class, throwingRunnable(npe)); } catch (AssertionError error) { assertEquals( "unexpected exception type thrown; expected:" @@ -893,9 +893,9 @@ public void expectThrowsUsesNameUponTypeMismatchWithAnonymousClass() { } @Test - public void expectThrowsUsesCanonicalNameWhenRequiredExceptionNotThrown() { + public void assertThrowsUsesCanonicalNameWhenRequiredExceptionNotThrown() { try { - expectThrows(NestedException.class, nonThrowingRunnable()); + assertThrows(NestedException.class, nonThrowingRunnable()); } catch (AssertionError error) { assertEquals( "expected org.junit.tests.assertion.AssertionTest.NestedException to be thrown," From 2fb2db180914eafd181edf942c33f071e706f1ab Mon Sep 17 00:00:00 2001 From: Kevin Cooney Date: Wed, 14 Dec 2016 07:41:22 -0800 Subject: [PATCH 2/2] Rename assertThrows() to expectThrows(), to be consistent wth ExpectedException. --- src/main/java/org/junit/Assert.java | 4 +- .../org/junit/function/ThrowingRunnable.java | 4 +- .../java/org/junit/rules/ErrorCollector.java | 4 +- .../org/junit/rules/ExpectedException.java | 4 +- .../junit/tests/assertion/AssertionTest.java | 38 +++++++++---------- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/junit/Assert.java b/src/main/java/org/junit/Assert.java index bb2c4bc93661..8d11ece9b11a 100755 --- a/src/main/java/org/junit/Assert.java +++ b/src/main/java/org/junit/Assert.java @@ -967,7 +967,7 @@ public static void assertThat(String reason, T actual, } /** - * Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when + * Checks 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 @@ -978,7 +978,7 @@ public static void assertThat(String reason, T actual, * @return the exception thrown by {@code runnable} * @since 4.13 */ - public static T assertThrows(Class expectedThrowable, ThrowingRunnable runnable) { + public static T expectThrows(Class expectedThrowable, ThrowingRunnable runnable) { try { runnable.run(); } catch (Throwable actualThrown) { diff --git a/src/main/java/org/junit/function/ThrowingRunnable.java b/src/main/java/org/junit/function/ThrowingRunnable.java index e8b095a5308b..2a1625d89e88 100644 --- a/src/main/java/org/junit/function/ThrowingRunnable.java +++ b/src/main/java/org/junit/function/ThrowingRunnable.java @@ -1,8 +1,8 @@ package org.junit.function; /** - * This interface facilitates the use of assertThrows from Java 8. It allows method references - * to void methods (that declare checked exceptions) to be passed directly into assertThrows + * This interface facilitates the use of {@code expectThrows} from Java 8. It allows method references + * to void methods (that declare checked exceptions) to be passed directly into {@code expectThrows}. * without wrapping. It is not meant to be implemented directly. * * @since 4.13 diff --git a/src/main/java/org/junit/rules/ErrorCollector.java b/src/main/java/org/junit/rules/ErrorCollector.java index 80a7c12712ef..2228de081dde 100644 --- a/src/main/java/org/junit/rules/ErrorCollector.java +++ b/src/main/java/org/junit/rules/ErrorCollector.java @@ -1,7 +1,7 @@ package org.junit.rules; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertThrows; +import static org.junit.Assert.expectThrows; import java.util.ArrayList; import java.util.List; @@ -117,7 +117,7 @@ public T checkSucceeds(Callable callable) { */ public void checkThrows(Class expectedThrowable, ThrowingRunnable runnable) { try { - assertThrows(expectedThrowable, runnable); + expectThrows(expectedThrowable, runnable); } catch (AssertionError e) { addError(e); } diff --git a/src/main/java/org/junit/rules/ExpectedException.java b/src/main/java/org/junit/rules/ExpectedException.java index 4457dab264fc..b20cbe097435 100644 --- a/src/main/java/org/junit/rules/ExpectedException.java +++ b/src/main/java/org/junit/rules/ExpectedException.java @@ -15,8 +15,8 @@ /** * The {@code ExpectedException} rule allows you to verify that your code * throws a specific exception. Note that, starting with Java 8, - * {@link org.junit.Assert#assertThrows(java.lang.Class, org.junit.function.ThrowingRunnable) - * Assert.assertThrows} + * {@link org.junit.Assert#expectThrows(java.lang.Class, org.junit.function.ThrowingRunnable) + * Assert.expectThrows} * is often a better choice since it allows you to express exactly where you * expect the exception to be thrown. * diff --git a/src/test/java/org/junit/tests/assertion/AssertionTest.java b/src/test/java/org/junit/tests/assertion/AssertionTest.java index ced5e00df8df..0c5746a6e817 100755 --- a/src/test/java/org/junit/tests/assertion/AssertionTest.java +++ b/src/test/java/org/junit/tests/assertion/AssertionTest.java @@ -10,8 +10,8 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.expectThrows; import static org.junit.Assert.fail; import java.io.IOException; @@ -797,14 +797,14 @@ public void assertNotEqualsIgnoresFloatDeltaOnNaN() { } @Test(expected = AssertionError.class) - public void assertThrowsRequiresAnExceptionToBeThrown() { - assertThrows(Throwable.class, nonThrowingRunnable()); + public void expectThrowsRequiresAnExceptionToBeThrown() { + expectThrows(Throwable.class, nonThrowingRunnable()); } @Test - public void assertThrowsIncludesAnInformativeDefaultMessage() { + public void expectThrowsIncludesAnInformativeDefaultMessage() { try { - assertThrows(Throwable.class, nonThrowingRunnable()); + expectThrows(Throwable.class, nonThrowingRunnable()); } catch (AssertionError ex) { assertEquals("expected java.lang.Throwable to be thrown, but nothing was thrown", ex.getMessage()); return; @@ -813,27 +813,27 @@ public void assertThrowsIncludesAnInformativeDefaultMessage() { } @Test - public void assertThrowsReturnsTheSameObjectThrown() { + public void expectThrowsReturnsTheSameObjectThrown() { NullPointerException npe = new NullPointerException(); - Throwable throwable = assertThrows(Throwable.class, throwingRunnable(npe)); + Throwable throwable = expectThrows(Throwable.class, throwingRunnable(npe)); assertSame(npe, throwable); } @Test(expected = AssertionError.class) - public void assertThrowsDetectsTypeMismatchesViaExplicitTypeHint() { + public void expectThrowsDetectsTypeMismatchesViaExplicitTypeHint() { NullPointerException npe = new NullPointerException(); - assertThrows(IOException.class, throwingRunnable(npe)); + expectThrows(IOException.class, throwingRunnable(npe)); } @Test - public void assertThrowsWrapsAndPropagatesUnexpectedExceptions() { + public void expectThrowsWrapsAndPropagatesUnexpectedExceptions() { NullPointerException npe = new NullPointerException("inner-message"); try { - assertThrows(IOException.class, throwingRunnable(npe)); + expectThrows(IOException.class, throwingRunnable(npe)); } catch (AssertionError ex) { assertSame(npe, ex.getCause()); assertEquals("inner-message", ex.getCause().getMessage()); @@ -843,11 +843,11 @@ public void assertThrowsWrapsAndPropagatesUnexpectedExceptions() { } @Test - public void assertThrowsSuppliesACoherentErrorMessageUponTypeMismatch() { + public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() { NullPointerException npe = new NullPointerException(); try { - assertThrows(IOException.class, throwingRunnable(npe)); + expectThrows(IOException.class, throwingRunnable(npe)); } catch (AssertionError error) { assertEquals("unexpected exception type thrown; expected: but was:", error.getMessage()); @@ -858,11 +858,11 @@ public void assertThrowsSuppliesACoherentErrorMessageUponTypeMismatch() { } @Test - public void assertThrowsUsesCanonicalNameUponTypeMismatch() { + public void expectThrowsUsesCanonicalNameUponTypeMismatch() { NullPointerException npe = new NullPointerException(); try { - assertThrows(NestedException.class, throwingRunnable(npe)); + expectThrows(NestedException.class, throwingRunnable(npe)); } catch (AssertionError error) { assertEquals( "unexpected exception type thrown; expected:" @@ -875,12 +875,12 @@ public void assertThrowsUsesCanonicalNameUponTypeMismatch() { } @Test - public void assertThrowsUsesNameUponTypeMismatchWithAnonymousClass() { + public void expectThrowsUsesNameUponTypeMismatchWithAnonymousClass() { NullPointerException npe = new NullPointerException() { }; try { - assertThrows(IOException.class, throwingRunnable(npe)); + expectThrows(IOException.class, throwingRunnable(npe)); } catch (AssertionError error) { assertEquals( "unexpected exception type thrown; expected:" @@ -893,9 +893,9 @@ public void assertThrowsUsesNameUponTypeMismatchWithAnonymousClass() { } @Test - public void assertThrowsUsesCanonicalNameWhenRequiredExceptionNotThrown() { + public void expectThrowsUsesCanonicalNameWhenRequiredExceptionNotThrown() { try { - assertThrows(NestedException.class, nonThrowingRunnable()); + expectThrows(NestedException.class, nonThrowingRunnable()); } catch (AssertionError error) { assertEquals( "expected org.junit.tests.assertion.AssertionTest.NestedException to be thrown,"