From bdbea615e76a2edb9c131d5919dd91344658e227 Mon Sep 17 00:00:00 2001 From: olcbean Date: Mon, 17 Dec 2018 22:35:27 +0100 Subject: [PATCH] Improve message of Assertions.assertDoesNotThrow If an unexpected error is thrown, the throwable message is added to the resulting `AssertionFailedError`. Closes #1703 --- .../junit/jupiter/api/AssertDoesNotThrow.java | 19 ++++-- .../AssertDoesNotThrowAssertionsTests.java | 59 +++++++++++++++++++ 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertDoesNotThrow.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertDoesNotThrow.java index 569a76b44c1d..2cd887c740e4 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertDoesNotThrow.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertDoesNotThrow.java @@ -17,6 +17,7 @@ import org.junit.jupiter.api.function.Executable; import org.junit.jupiter.api.function.ThrowingSupplier; +import org.junit.platform.commons.util.StringUtils; import org.opentest4j.AssertionFailedError; /** @@ -48,9 +49,7 @@ private static void assertDoesNotThrow(Executable executable, Object messageOrSu executable.execute(); } catch (Throwable t) { - String message = buildPrefix(nullSafeGet(messageOrSupplier)) + "Unexpected exception thrown: " - + t.getClass().getName(); - throw new AssertionFailedError(message, t); + throw createAssertionFailedError(messageOrSupplier, t); } } @@ -71,10 +70,18 @@ private static T assertDoesNotThrow(ThrowingSupplier supplier, Object mes return supplier.get(); } catch (Throwable t) { - String message = buildPrefix(nullSafeGet(messageOrSupplier)) + "Unexpected exception thrown: " - + t.getClass().getName(); - throw new AssertionFailedError(message, t); + throw createAssertionFailedError(messageOrSupplier, t); } } + private static AssertionFailedError createAssertionFailedError(Object messageOrSupplier, Throwable t) { + String message = buildPrefix(nullSafeGet(messageOrSupplier)) + "Unexpected exception thrown: " + + t.getClass().getName() + addThrowableMessage(t); + return new AssertionFailedError(message, t); + } + + private static String addThrowableMessage(Throwable t) { + return StringUtils.isBlank(t.getMessage()) ? "" : ": " + t.getMessage(); + } + } diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/AssertDoesNotThrowAssertionsTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/AssertDoesNotThrowAssertionsTests.java index 303565092f69..57d7dc3d0e32 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/AssertDoesNotThrowAssertionsTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/AssertDoesNotThrowAssertionsTests.java @@ -107,6 +107,20 @@ void assertDoesNotThrowWithExecutableThatThrowsACheckedException() { } } + @Test + void assertDoesNotThrowWithExecutableThatThrowsACheckedExceptionWithMessage() { + String message = "Checked exception message"; + try { + assertDoesNotThrow((Executable) () -> { + throw new IOException(message); + }); + expectAssertionFailedError(); + } + catch (AssertionFailedError ex) { + assertMessageEquals(ex, "Unexpected exception thrown: " + IOException.class.getName() + ": " + message); + } + } + @Test void assertDoesNotThrowWithExecutableThatThrowsARuntimeException() { try { @@ -120,6 +134,21 @@ void assertDoesNotThrowWithExecutableThatThrowsARuntimeException() { } } + @Test + void assertDoesNotThrowWithExecutableThatThrowsARuntimeExceptionWithMessage() { + String message = "Runtime exception message"; + try { + assertDoesNotThrow((Executable) () -> { + throw new IllegalStateException(message); + }); + expectAssertionFailedError(); + } + catch (AssertionFailedError ex) { + assertMessageEquals(ex, + "Unexpected exception thrown: " + IllegalStateException.class.getName() + ": " + message); + } + } + @Test void assertDoesNotThrowWithExecutableThatThrowsAnError() { try { @@ -145,6 +174,21 @@ void assertDoesNotThrowWithExecutableThatThrowsAnExceptionWithMessageString() { } } + @Test + void assertDoesNotThrowWithExecutableThatThrowsAnExceptionWithMessageWithMessageString() { + String message = "Runtime exception message"; + try { + assertDoesNotThrow((Executable) () -> { + throw new IllegalStateException(message); + }, "Custom message"); + expectAssertionFailedError(); + } + catch (AssertionFailedError ex) { + assertMessageEquals(ex, "Custom message ==> Unexpected exception thrown: " + + IllegalStateException.class.getName() + ": " + message); + } + } + @Test void assertDoesNotThrowWithExecutableThatThrowsAnExceptionWithMessageSupplier() { try { @@ -159,6 +203,21 @@ void assertDoesNotThrowWithExecutableThatThrowsAnExceptionWithMessageSupplier() } } + @Test + void assertDoesNotThrowWithExecutableThatThrowsAnExceptionWithMessageWithMessageSupplier() { + String message = "Runtime exception message"; + try { + assertDoesNotThrow((Executable) () -> { + throw new IllegalStateException(message); + }, () -> "Custom message"); + expectAssertionFailedError(); + } + catch (AssertionFailedError ex) { + assertMessageEquals(ex, "Custom message ==> Unexpected exception thrown: " + + IllegalStateException.class.getName() + ": " + message); + } + } + // --- supplier ------------------------------------------------------------ @Test