Skip to content

Commit

Permalink
Improve message of Assertions.assertDoesNotThrow
Browse files Browse the repository at this point in the history
If an unexpected error is thrown, the throwable message is added
to the resulting `AssertionFailedError`.

Closes #1703
  • Loading branch information
olcbean authored and sormuras committed Dec 17, 2018
1 parent 5f40c6c commit bdbea61
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -71,10 +70,18 @@ private static <T> T assertDoesNotThrow(ThrowingSupplier<T> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit bdbea61

Please sign in to comment.