Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,6 +30,8 @@
*/
class AssertionUtils {

private static final Logger logger = LoggerFactory.getLogger(AssertionUtils.class);

private AssertionUtils() {
/* no-op */
}
Expand Down Expand Up @@ -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: <TODO>");
}
return obj1.equals(obj2);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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: <TODO>
""".lines(), listener.stream(AssertionUtils.class).map(LogRecord::getMessage));
}
}

// -------------------------------------------------------------------------

@SuppressWarnings("overrides")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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: <TODO>
""".lines(), listener.stream(AssertionUtils.class).map(LogRecord::getMessage));
}
}

// -------------------------------------------------------------------------

@Nested
Expand Down
Loading