Skip to content

Commit 3adea80

Browse files
committed
Add self-tests for assert[Not]Equals() with arrays
1 parent 575f741 commit 3adea80

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import org.jspecify.annotations.Nullable;
2020
import org.junit.platform.commons.annotation.Contract;
21+
import org.junit.platform.commons.logging.Logger;
22+
import org.junit.platform.commons.logging.LoggerFactory;
2123
import org.junit.platform.commons.util.UnrecoverableExceptions;
2224

2325
/**
@@ -28,6 +30,8 @@
2830
*/
2931
class AssertionUtils {
3032

33+
private static final Logger logger = LoggerFactory.getLogger(AssertionUtils.class);
34+
3135
private AssertionUtils() {
3236
/* no-op */
3337
}
@@ -129,6 +133,10 @@ static boolean objectsAreEqual(@Nullable Object obj1, @Nullable Object obj2) {
129133
if (obj1 == null) {
130134
return (obj2 == null);
131135
}
136+
if (obj1.getClass().isArray() && (obj2 != null && obj2.getClass().isArray())) {
137+
// TODO Find first method in user's code, i.e. non-framework code.
138+
logger.debug(() -> "Should have used `assertArrayEquals()` in method: <TODO>");
139+
}
132140
return obj1.equals(obj2);
133141
}
134142

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@
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;
20+
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
1921
import static org.junit.jupiter.api.Assertions.assertThrows;
2022

23+
import java.util.logging.LogRecord;
24+
25+
import org.junit.jupiter.api.fixtures.TrackLogRecords;
2126
import org.junit.jupiter.api.function.Executable;
27+
import org.junit.platform.commons.logging.LogRecordListener;
2228
import org.opentest4j.AssertionFailedError;
2329

2430
/**
@@ -750,6 +756,35 @@ void chars() {
750756

751757
}
752758

759+
@Nested
760+
class ArraysAsArguments {
761+
@Test
762+
void objects(@TrackLogRecords LogRecordListener listener) {
763+
Object object = new Object();
764+
Object array1 = new Object[] { object };
765+
Object array2 = new Object[] { object };
766+
try {
767+
assertEquals(array1, array2);
768+
expectAssertionFailedError();
769+
}
770+
catch (AssertionFailedError ex) {
771+
assertMessageMatches(ex, "expected: " + //
772+
"\\Q[Ljava.lang.Object;@\\E" + //
773+
".+" + //
774+
"\\Q<[java.lang.Object@\\E" + //
775+
".+" + //
776+
"\\Q]> but was: [Ljava.lang.Object;@\\E" + //
777+
".+" + //
778+
"\\Q<[java.lang.Object@\\E" + //
779+
".+" + //
780+
"\\Q]>\\E");
781+
}
782+
assertLinesMatch("""
783+
Should have used `assertArrayEquals()` in method: <TODO>
784+
""".lines(), listener.stream(AssertionUtils.class).map(LogRecord::getMessage));
785+
}
786+
}
787+
753788
// -------------------------------------------------------------------------
754789

755790
@SuppressWarnings("overrides")

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageEquals;
1515
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageStartsWith;
1616
import static org.junit.jupiter.api.AssertionTestUtils.expectAssertionFailedError;
17+
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
1718
import static org.junit.jupiter.api.Assertions.assertNotEquals;
1819
import static org.junit.jupiter.api.Assertions.assertThrows;
1920

21+
import java.util.logging.LogRecord;
22+
23+
import org.junit.jupiter.api.fixtures.TrackLogRecords;
24+
import org.junit.platform.commons.logging.LogRecordListener;
2025
import org.opentest4j.AssertionFailedError;
2126

2227
/**
@@ -624,6 +629,20 @@ void assertNotEqualsInvokesEqualsMethodForIdenticalObjects() {
624629

625630
}
626631

632+
@Nested
633+
class AssertNotEqualsArrays {
634+
@Test
635+
void objects(@TrackLogRecords LogRecordListener listener) {
636+
Object object = new Object();
637+
Object array1 = new Object[] { object };
638+
Object array2 = new Object[] { object };
639+
assertNotEquals(array1, array2);
640+
assertLinesMatch("""
641+
Should have used `assertArrayEquals()` in method: <TODO>
642+
""".lines(), listener.stream(AssertionUtils.class).map(LogRecord::getMessage));
643+
}
644+
}
645+
627646
// -------------------------------------------------------------------------
628647

629648
@Nested

0 commit comments

Comments
 (0)