Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix assertEquals() if toString() returns null #1454

Merged
merged 5 commits into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kcooney I would like this extracted as a function, and will update other tests then separately.

Copy link
Member

Choose a reason for hiding this comment

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

If we could could do that without hurting readability that is fine. For tests I am fine with some duplication if it makes the code clear.

}

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