Skip to content

Commit dd5e34a

Browse files
committed
Add self-tests for assert[Not]Equals() with arrays
1 parent 88fbb96 commit dd5e34a

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ static boolean objectsAreEqual(@Nullable Object obj1, @Nullable Object obj2) {
114114
if (obj1 == null) {
115115
return (obj2 == null);
116116
}
117+
// if (obj1.getClass().isArray() && (obj2 != null && obj2.getClass().isArray())) {
118+
// throw new AssertionError("Should have used `assertArrayEquals()?!`");
119+
// }
117120
return obj1.equals(obj2);
118121
}
119122

jupiter-tests/src/test/java/org/junit/jupiter/api/AssertEqualsAssertionsTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static org.junit.jupiter.api.AssertionTestUtils.assertExpectedAndActualValues;
1414
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageEndsWith;
1515
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageEquals;
16+
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageMatches;
1617
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageStartsWith;
1718
import static org.junit.jupiter.api.AssertionTestUtils.expectAssertionFailedError;
1819
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -750,6 +751,31 @@ void chars() {
750751

751752
}
752753

754+
@Nested
755+
class ArraysAsArguments {
756+
@Test
757+
void objects() {
758+
Object object = new Object();
759+
Object array1 = new Object[] { object };
760+
Object array2 = new Object[] { object };
761+
try {
762+
assertEquals(array1, array2);
763+
expectAssertionFailedError();
764+
}
765+
catch (AssertionFailedError ex) {
766+
assertMessageMatches(ex, "expected: " + //
767+
"\\Q[Ljava.lang.Object;@\\E" + //
768+
".+" + //
769+
"\\Q<[java.lang.Object@\\E.+" + //
770+
"\\Q]> but was: [Ljava.lang.Object;@\\E" + //
771+
".+" + //
772+
"\\Q<[java.lang.Object@\\E" + //
773+
".+" + //
774+
"\\Q]>\\E");
775+
}
776+
}
777+
}
778+
753779
// -------------------------------------------------------------------------
754780

755781
@SuppressWarnings("overrides")

jupiter-tests/src/test/java/org/junit/jupiter/api/AssertNotEqualsAssertionsTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,17 @@ void assertNotEqualsInvokesEqualsMethodForIdenticalObjects() {
624624

625625
}
626626

627+
@Nested
628+
class AssertNotEqualsArrays {
629+
@Test
630+
void objects() {
631+
Object object = new Object();
632+
Object array1 = new Object[] { object };
633+
Object array2 = new Object[] { object };
634+
assertNotEquals(array1, array2);
635+
}
636+
}
637+
627638
// -------------------------------------------------------------------------
628639

629640
@Nested

0 commit comments

Comments
 (0)