From dd5e34a61d631402ef1bd2714c59c24ea9493ad1 Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Mon, 1 Sep 2025 16:48:03 +0200 Subject: [PATCH 1/2] Add self-tests for `assert[Not]Equals()` with arrays --- .../org/junit/jupiter/api/AssertionUtils.java | 3 +++ .../api/AssertEqualsAssertionsTests.java | 26 +++++++++++++++++++ .../api/AssertNotEqualsAssertionsTests.java | 11 ++++++++ 3 files changed, 40 insertions(+) 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..e1771767e618 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 @@ -114,6 +114,9 @@ static boolean objectsAreEqual(@Nullable Object obj1, @Nullable Object obj2) { if (obj1 == null) { return (obj2 == null); } + // if (obj1.getClass().isArray() && (obj2 != null && obj2.getClass().isArray())) { + // throw new AssertionError("Should have used `assertArrayEquals()?!`"); + // } 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..bdcf4e8672a2 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,6 +13,7 @@ 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; @@ -750,6 +751,31 @@ void chars() { } + @Nested + class ArraysAsArguments { + @Test + void objects() { + 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"); + } + } + } + // ------------------------------------------------------------------------- @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..203319656892 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 @@ -624,6 +624,17 @@ void assertNotEqualsInvokesEqualsMethodForIdenticalObjects() { } + @Nested + class AssertNotEqualsArrays { + @Test + void objects() { + Object object = new Object(); + Object array1 = new Object[] { object }; + Object array2 = new Object[] { object }; + assertNotEquals(array1, array2); + } + } + // ------------------------------------------------------------------------- @Nested From bdbd6ff473bfdaacb24718959852f6868b5ad5d4 Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Tue, 2 Sep 2025 10:24:00 +0200 Subject: [PATCH 2/2] Add and assert logging statement --- .../java/org/junit/jupiter/api/AssertionUtils.java | 11 ++++++++--- .../jupiter/api/AssertEqualsAssertionsTests.java | 13 +++++++++++-- .../jupiter/api/AssertNotEqualsAssertionsTests.java | 10 +++++++++- 3 files changed, 28 insertions(+), 6 deletions(-) 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 e1771767e618..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,9 +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())) { - // throw new AssertionError("Should have used `assertArrayEquals()?!`"); - // } + 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 bdcf4e8672a2..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 @@ -17,9 +17,14 @@ 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; /** @@ -754,7 +759,7 @@ void chars() { @Nested class ArraysAsArguments { @Test - void objects() { + void objects(@TrackLogRecords LogRecordListener listener) { Object object = new Object(); Object array1 = new Object[] { object }; Object array2 = new Object[] { object }; @@ -766,13 +771,17 @@ void objects() { assertMessageMatches(ex, "expected: " + // "\\Q[Ljava.lang.Object;@\\E" + // ".+" + // - "\\Q<[java.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)); } } 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 203319656892..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; /** @@ -627,11 +632,14 @@ void assertNotEqualsInvokesEqualsMethodForIdenticalObjects() { @Nested class AssertNotEqualsArrays { @Test - void objects() { + 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)); } }