Skip to content

Commit

Permalink
Improve failure message for null vs. null comparisons
Browse files Browse the repository at this point in the history
Prior to this commit, if the expected and actual values for a failed
assertion both had "null" string representations and if either the
expected or actual value was the null reference, the generated failure
message looked something like the following.

    expected: null<null> but was: java.lang.String@264b3504<null>

This commit improves the assertion failure message in such cases by
representing the null reference as "<null>" instead of "null<null>",
leading to generated failure messages similar to the following.

    expected: <null> but was: java.lang.String@264b3504<null>

This improvement applies to assertEquals(), assertNull, and any other
assertion that internally delegates to format(Object, Object, String) or
formatValues(Object, Object) in AssertionUtils.

See #2523
  • Loading branch information
sbrannen committed Jan 31, 2021
1 parent eab6490 commit 68bcfdd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ static String formatValues(Object expected, Object actual) {
}

private static String formatClassAndValue(Object value, String valueString) {
// If the value is null, return <null> instead of null<null>.
if (value == null) {
return "<null>";
}
String classAndHash = getClassName(value) + toHash(value);
// if it's a class, there's no need to repeat the class name contained in the valueString.
return (value instanceof Class ? "<" + classAndHash + ">" : classAndHash + "<" + valueString + ">");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,32 @@ void assertEqualsWithObjectVsNull() {
}
}

@Test
void assertEqualsWithObjectWithNullStringReturnedFromToStringVsNull() {
try {
assertEquals("null", null);
expectAssertionFailedError();
}
catch (AssertionFailedError ex) {
assertMessageStartsWith(ex, "expected: java.lang.String@");
assertMessageEndsWith(ex, "<null> but was: <null>");
assertExpectedAndActualValues(ex, "null", null);
}
}

@Test
void assertEqualsWithNullVsObjectWithNullStringReturnedFromToString() {
try {
assertEquals(null, "null");
expectAssertionFailedError();
}
catch (AssertionFailedError ex) {
assertMessageStartsWith(ex, "expected: <null> but was: java.lang.String@");
assertMessageEndsWith(ex, "<null>");
assertExpectedAndActualValues(ex, null, "null");
}
}

@Test
void assertEqualsWithNullVsObjectAndMessageSupplier() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ private void assertNullWithNonNullObjectWithNullStringReturnedFromToString(Suppl
}
catch (AssertionFailedError ex) {
// Should look something like:
// expected: null<null> but was: java.lang.String@264b3504<null>
// expected: <null> but was: java.lang.String@264b3504<null>
String prefix = (messageSupplier != null ? messageSupplier.get() + " ==> " : "");
assertMessageStartsWith(ex, prefix + "expected: null<null> but was: java.lang.String@");
assertMessageStartsWith(ex, prefix + "expected: <null> but was: java.lang.String@");
assertMessageEndsWith(ex, "<null>");
assertExpectedAndActualValues(ex, null, actual);
}
Expand Down Expand Up @@ -105,10 +105,10 @@ private void assertNullWithNonNullObjectWithNullReferenceReturnedFromToString(Su
}
catch (AssertionFailedError ex) {
// Should look something like:
// expected: null<null> but was: org.junit.jupiter.api.AssertNullAssertionsTests$NullToString@4e7912d8<null>
// expected: <null> but was: org.junit.jupiter.api.AssertNullAssertionsTests$NullToString@4e7912d8<null>
String prefix = (messageSupplier != null ? messageSupplier.get() + " ==> " : "");
assertMessageStartsWith(ex,
prefix + "expected: null<null> but was: org.junit.jupiter.api.AssertNullAssertionsTests$NullToString@");
prefix + "expected: <null> but was: org.junit.jupiter.api.AssertNullAssertionsTests$NullToString@");
assertMessageEndsWith(ex, "<null>");
assertExpectedAndActualValues(ex, null, actual);
}
Expand Down

1 comment on commit 68bcfdd

@marcphilipp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 👍

Please sign in to comment.