diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionUtils.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionUtils.java index f5dae75d401f..72e995e8b205 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionUtils.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionUtils.java @@ -17,6 +17,8 @@ import org.jspecify.annotations.Nullable; import org.junit.platform.commons.annotation.Contract; +import org.junit.platform.commons.logging.Logger; +import org.junit.platform.commons.logging.LoggerFactory; import org.junit.platform.commons.util.UnrecoverableExceptions; import org.opentest4j.AssertionFailedError; @@ -28,6 +30,8 @@ */ class AssertionUtils { + private static final Logger logger = LoggerFactory.getLogger(AssertionUtils.class); + private AssertionUtils() { /* no-op */ } @@ -114,6 +118,10 @@ static boolean objectsAreEqual(@Nullable Object obj1, @Nullable Object obj2) { if (obj1 == null) { return (obj2 == null); } + if (obj1.getClass().isArray() && (obj2 != null && obj2.getClass().isArray())) { + // TODO Find first method in user's code, i.e. non-framework code. + logger.debug(() -> "Should have used `assertArrayEquals()` in method: "); + } return obj1.equals(obj2); } diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertEqualsAssertionsTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertEqualsAssertionsTests.java index ec30168e7605..8612e5ff6444 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertEqualsAssertionsTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertEqualsAssertionsTests.java @@ -13,12 +13,18 @@ import static org.junit.jupiter.api.AssertionTestUtils.assertExpectedAndActualValues; import static org.junit.jupiter.api.AssertionTestUtils.assertMessageEndsWith; import static org.junit.jupiter.api.AssertionTestUtils.assertMessageEquals; +import static org.junit.jupiter.api.AssertionTestUtils.assertMessageMatches; import static org.junit.jupiter.api.AssertionTestUtils.assertMessageStartsWith; import static org.junit.jupiter.api.AssertionTestUtils.expectAssertionFailedError; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertLinesMatch; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.util.logging.LogRecord; + +import org.junit.jupiter.api.fixtures.TrackLogRecords; import org.junit.jupiter.api.function.Executable; +import org.junit.platform.commons.logging.LogRecordListener; import org.opentest4j.AssertionFailedError; /** @@ -750,6 +756,35 @@ void chars() { } + @Nested + class ArraysAsArguments { + @Test + void objects(@TrackLogRecords LogRecordListener listener) { + Object object = new Object(); + Object array1 = new Object[] { object }; + Object array2 = new Object[] { object }; + try { + assertEquals(array1, array2); + expectAssertionFailedError(); + } + catch (AssertionFailedError ex) { + assertMessageMatches(ex, "expected: " + // + "\\Q[Ljava.lang.Object;@\\E" + // + ".+" + // + "\\Q<[java.lang.Object@\\E" + // + ".+" + // + "\\Q]> but was: [Ljava.lang.Object;@\\E" + // + ".+" + // + "\\Q<[java.lang.Object@\\E" + // + ".+" + // + "\\Q]>\\E"); + } + assertLinesMatch(""" + Should have used `assertArrayEquals()` in method: + """.lines(), listener.stream(AssertionUtils.class).map(LogRecord::getMessage)); + } + } + // ------------------------------------------------------------------------- @SuppressWarnings("overrides") diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertNotEqualsAssertionsTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertNotEqualsAssertionsTests.java index 842e7f463a8f..48a55768ded4 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertNotEqualsAssertionsTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/api/AssertNotEqualsAssertionsTests.java @@ -14,9 +14,14 @@ import static org.junit.jupiter.api.AssertionTestUtils.assertMessageEquals; import static org.junit.jupiter.api.AssertionTestUtils.assertMessageStartsWith; import static org.junit.jupiter.api.AssertionTestUtils.expectAssertionFailedError; +import static org.junit.jupiter.api.Assertions.assertLinesMatch; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.util.logging.LogRecord; + +import org.junit.jupiter.api.fixtures.TrackLogRecords; +import org.junit.platform.commons.logging.LogRecordListener; import org.opentest4j.AssertionFailedError; /** @@ -624,6 +629,20 @@ void assertNotEqualsInvokesEqualsMethodForIdenticalObjects() { } + @Nested + class AssertNotEqualsArrays { + @Test + void objects(@TrackLogRecords LogRecordListener listener) { + Object object = new Object(); + Object array1 = new Object[] { object }; + Object array2 = new Object[] { object }; + assertNotEquals(array1, array2); + assertLinesMatch(""" + Should have used `assertArrayEquals()` in method: + """.lines(), listener.stream(AssertionUtils.class).map(LogRecord::getMessage)); + } + } + // ------------------------------------------------------------------------- @Nested