Skip to content

Commit

Permalink
Improve error message for assertArrayEquals() when multi-dimensional …
Browse files Browse the repository at this point in the history
…arrays

have different lengths.

Previously, JUnit's assertion error message would indicate only that some array
lengths x and y were unequal, without indicating whether this pertained to the
outer array or some nested array. Now, in case of a length mismatch between two
nested arrays, Junit will tell at which indices they reside.

Closes #1054
Closes #803
  • Loading branch information
Stephan202 authored and kcooney committed Jan 4, 2015
1 parent e4c92d4 commit 3f0adea
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main/java/org/junit/internal/ComparisonCriteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public abstract class ComparisonCriteria {
*/
public void arrayEquals(String message, Object expecteds, Object actuals)
throws ArrayComparisonFailure {
arrayEquals(message, expecteds, actuals, true);
}

private void arrayEquals(String message, Object expecteds, Object actuals, boolean outer)
throws ArrayComparisonFailure {
if (expecteds == actuals
|| Arrays.deepEquals(new Object[] {expecteds}, new Object[] {actuals})) {
// The reflection-based loop below is potentially very slow, especially for primitive
Expand All @@ -34,19 +39,23 @@ public void arrayEquals(String message, Object expecteds, Object actuals)
}
String header = message == null ? "" : message + ": ";

int expectedsLength = assertArraysAreSameLength(expecteds,
actuals, header);
// Only include the user-provided message in the outer exception.
String exceptionMessage = outer ? header : "";
int expectedsLength = assertArraysAreSameLength(expecteds, actuals, exceptionMessage);

for (int i = 0; i < expectedsLength; i++) {
Object expected = Array.get(expecteds, i);
Object actual = Array.get(actuals, i);

if (isArray(expected) && isArray(actual)) {
try {
arrayEquals(message, expected, actual);
arrayEquals(message, expected, actual, false);
} catch (ArrayComparisonFailure e) {
e.addDimension(i);
throw e;
} catch (AssertionError e) {
// Array lengths differed.
throw new ArrayComparisonFailure(header, e, i);
}
} else {
try {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/junit/tests/assertion/AssertionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,30 @@ public void multiDimensionalArraysAreNotEqualNoMessage() {
}
}

@Test
public void multiDimensionalArraysDifferentLengthMessage() {
try {
assertArrayEquals("message", new Object[][]{{true, true}, {false, false}}, new Object[][]{{true, true}, {false}});
} catch (AssertionError exception) {
assertEquals("message: arrays first differed at element [1]; array lengths differed, expected.length=2 actual.length=1", exception.getMessage());
return;
}

fail("Expected AssertionError to be thrown");
}

@Test
public void multiDimensionalArraysDifferentLengthNoMessage() {
try {
assertArrayEquals(new Object[][]{{true, true}, {false, false}}, new Object[][]{{true, true}, {false}});
} catch (AssertionError exception) {
assertEquals("arrays first differed at element [1]; array lengths differed, expected.length=2 actual.length=1", exception.getMessage());
return;
}

fail("Expected AssertionError to be thrown");
}

@Test
public void arraysWithNullElementEqual() {
Object[] objects1 = new Object[]{null};
Expand Down

0 comments on commit 3f0adea

Please sign in to comment.