Skip to content

Commit bdbd6ff

Browse files
committed
Add and assert logging statement
1 parent dd5e34a commit bdbd6ff

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import org.jspecify.annotations.Nullable;
1919
import org.junit.platform.commons.annotation.Contract;
20+
import org.junit.platform.commons.logging.Logger;
21+
import org.junit.platform.commons.logging.LoggerFactory;
2022
import org.junit.platform.commons.util.UnrecoverableExceptions;
2123
import org.opentest4j.AssertionFailedError;
2224

@@ -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
}
@@ -114,9 +118,10 @@ static boolean objectsAreEqual(@Nullable Object obj1, @Nullable Object obj2) {
114118
if (obj1 == null) {
115119
return (obj2 == null);
116120
}
117-
// if (obj1.getClass().isArray() && (obj2 != null && obj2.getClass().isArray())) {
118-
// throw new AssertionError("Should have used `assertArrayEquals()?!`");
119-
// }
121+
if (obj1.getClass().isArray() && (obj2 != null && obj2.getClass().isArray())) {
122+
// TODO Find first method in user's code, i.e. non-framework code.
123+
logger.debug(() -> "Should have used `assertArrayEquals()` in method: <TODO>");
124+
}
120125
return obj1.equals(obj2);
121126
}
122127

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageStartsWith;
1818
import static org.junit.jupiter.api.AssertionTestUtils.expectAssertionFailedError;
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
2021
import static org.junit.jupiter.api.Assertions.assertThrows;
2122

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

2530
/**
@@ -754,7 +759,7 @@ void chars() {
754759
@Nested
755760
class ArraysAsArguments {
756761
@Test
757-
void objects() {
762+
void objects(@TrackLogRecords LogRecordListener listener) {
758763
Object object = new Object();
759764
Object array1 = new Object[] { object };
760765
Object array2 = new Object[] { object };
@@ -766,13 +771,17 @@ void objects() {
766771
assertMessageMatches(ex, "expected: " + //
767772
"\\Q[Ljava.lang.Object;@\\E" + //
768773
".+" + //
769-
"\\Q<[java.lang.Object@\\E.+" + //
774+
"\\Q<[java.lang.Object@\\E" + //
775+
".+" + //
770776
"\\Q]> but was: [Ljava.lang.Object;@\\E" + //
771777
".+" + //
772778
"\\Q<[java.lang.Object@\\E" + //
773779
".+" + //
774780
"\\Q]>\\E");
775781
}
782+
assertLinesMatch("""
783+
Should have used `assertArrayEquals()` in method: <TODO>
784+
""".lines(), listener.stream(AssertionUtils.class).map(LogRecord::getMessage));
776785
}
777786
}
778787

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

Lines changed: 9 additions & 1 deletion
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
/**
@@ -627,11 +632,14 @@ void assertNotEqualsInvokesEqualsMethodForIdenticalObjects() {
627632
@Nested
628633
class AssertNotEqualsArrays {
629634
@Test
630-
void objects() {
635+
void objects(@TrackLogRecords LogRecordListener listener) {
631636
Object object = new Object();
632637
Object array1 = new Object[] { object };
633638
Object array2 = new Object[] { object };
634639
assertNotEquals(array1, array2);
640+
assertLinesMatch("""
641+
Should have used `assertArrayEquals()` in method: <TODO>
642+
""".lines(), listener.stream(AssertionUtils.class).map(LogRecord::getMessage));
635643
}
636644
}
637645

0 commit comments

Comments
 (0)