Skip to content

Commit

Permalink
Fix assertEquals() if toString() returns null (junit-team#1454)
Browse files Browse the repository at this point in the history
  • Loading branch information
panchenko authored and kcooney committed May 19, 2017
1 parent d07ed0b commit a81c5e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/junit/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ static String format(String message, Object expected, Object actual) {
}
String expectedString = String.valueOf(expected);
String actualString = String.valueOf(actual);
if (expectedString.equals(actualString)) {
if (equalsRegardingNull(expectedString, actualString)) {
return formatted + "expected: "
+ formatClassAndValue(expected, expectedString)
+ " but was: " + formatClassAndValue(actual, actualString);
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/junit/tests/assertion/AssertionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,27 @@ public void stringNullAndNullWorksToo() {
assertEquals("null", null);
}

private static class NullToString {
@Override
public String toString() {
return null;
}
}

@Test
public void nullToString() {
try {
assertEquals(new NullToString(), new NullToString());
} catch (AssertionError e) {
assertEquals("expected: org.junit.tests.assertion.AssertionTest$NullToString<null> but "
+ "was: org.junit.tests.assertion.AssertionTest$NullToString<null>",
e.getMessage());
return;
}

fail("AssertionError expected");
}

@Test(expected = AssertionError.class)
public void compareBigDecimalAndInteger() {
final BigDecimal bigDecimal = new BigDecimal("1.2");
Expand Down

0 comments on commit a81c5e2

Please sign in to comment.