Skip to content

Commit

Permalink
Fix bug in AssertionUtils.toString(Object)
Browse files Browse the repository at this point in the history
Prior to this commit, AssertionUtils.toString(Object) printed an array via
the array's toString() method which resulted in non-user-friendly output.

This commit addresses this issue by printing arrays using Arrays.toString(),
which produces human readable output.

This is a prerequisite for junit-team#961.

Issue: junit-team#1030
  • Loading branch information
jbduncan authored and Andrei94 committed Jun 23, 2018
1 parent ca38f09 commit a369275
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bin/
*.iws
*.uml
.idea/
*/out/*

# Misc
*.log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static java.util.stream.Collectors.joining;

import java.util.Arrays;
import java.util.Deque;
import java.util.function.Supplier;

Expand Down Expand Up @@ -91,7 +92,13 @@ private static String formatClassAndValue(Object value, String valueString) {
}

private static String toString(Object obj) {
return (obj instanceof Class ? getCanonicalName((Class<?>) obj) : String.valueOf(obj));
if (obj instanceof Class) {
return getCanonicalName((Class<?>) obj);
}
if (obj instanceof Object[]) {
return Arrays.toString((Object[]) obj);
}
return StringUtils.nullSafeToString(obj);
}

private static String toHash(Object obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1867,15 +1867,13 @@ void assertArrayEqualsDifferentNestedObjectArrays() {
assertMessageEquals(ex, "array contents differ at index [2][1][1][0], expected: <false> but was: <true>");
}

Object[] differentElement = new Object[] {};
try {
assertArrayEquals(new Object[] { 1, 2, 3, new Object[] { new Object[] { 4, new Object[] { 5 } } } },
new Object[] { 1, 2, 3, new Object[] { new Object[] { 4, new Object[] { differentElement } } } });
new Object[] { 1, 2, 3, new Object[] { new Object[] { 4, new Object[] { new Object[] {} } } } });
expectAssertionFailedError();
}
catch (AssertionFailedError ex) {
assertMessageEquals(ex,
"array contents differ at index [3][0][1][0], expected: <5> but was: <" + differentElement + ">");
assertMessageEquals(ex, "array contents differ at index [3][0][1][0], expected: <5> but was: <[]>");
}
}

Expand Down

0 comments on commit a369275

Please sign in to comment.