Clarify assertSame message for equivalent objects#3282
Conversation
📝 WalkthroughWalkthrough
ChangesassertSame failure message improvement
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@testng-asserts/src/main/java/org/testng/Assert.java`:
- Around line 1586-1594: The assertSame method is calling areEqual(actual,
expected) in the failure formatting block, which executes user-defined equals()
logic. Since assertSame should only perform identity checks (using ==), not
equality checks, replace the areEqual(actual, expected) call with a direct
identity comparison using the == operator. This ensures that user-defined
equals() methods are never invoked in assertSame, preventing unexpected
exceptions from changing assertion behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 65ea2ee7-ff2c-4a70-b148-e63d125b1aa7
📒 Files selected for processing (3)
CHANGES.txttestng-asserts/src/main/java/org/testng/Assert.javatestng-asserts/src/test/java/org/testng/AssertTest.java
| if (areEqual(actual, expected)) { | ||
| fail( | ||
| formatted | ||
| + ASSERT_EQUAL_LEFT | ||
| + expected | ||
| + "] but found equivalent but not same [" | ||
| + actual | ||
| + "]"); | ||
| } |
There was a problem hiding this comment.
Avoid executing user equals() logic in assertSame failure formatting.
assertSame is identity-only, but this new path evaluates areEqual(actual, expected), which calls user-defined equals(). If equals() throws, assertSame now throws that exception instead of an AssertionError, changing assertion behavior.
Suggested fix
private static void failNotSame(Object actual, Object expected, String message) {
String formatted = "";
if (message != null) {
formatted = message + " ";
}
- if (areEqual(actual, expected)) {
+ boolean equivalentButNotSame = false;
+ try {
+ equivalentButNotSame = areEqual(actual, expected);
+ } catch (RuntimeException ignored) {
+ // Keep assertSame semantics stable: failure reporting must not depend on user equals().
+ }
+ if (equivalentButNotSame) {
fail(
formatted
+ ASSERT_EQUAL_LEFT
+ expected
+ "] but found equivalent but not same ["
+ actual
+ "]");
}
fail(formatted + ASSERT_EQUAL_LEFT + expected + ASSERT_MIDDLE + actual + ASSERT_RIGHT);
}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@testng-asserts/src/main/java/org/testng/Assert.java` around lines 1586 -
1594, The assertSame method is calling areEqual(actual, expected) in the failure
formatting block, which executes user-defined equals() logic. Since assertSame
should only perform identity checks (using ==), not equality checks, replace the
areEqual(actual, expected) call with a direct identity comparison using the ==
operator. This ensures that user-defined equals() methods are never invoked in
assertSame, preventing unexpected exceptions from changing assertion behavior.
|
Thanks for the contribution but check #3278 which replaces internal logic by assertj |
|
Thanks for pointing me to #3278. I checked it and it looks like this PR is superseded by the AssertJ-backed assertion refactor, since assertSame/assertNotSame failure messages will come from AssertJ going forward. I’ll close this PR to avoid duplicate/conflicting work. Thanks for the review. |
Fixes testng-team/testng-asserts#10.
This clarifies the
assertSamefailure message when two objects are equivalent but not the same reference.Validation:
.\gradlew.bat :testng-asserts:test: PASSgit diff --check: PASSgit show --check: PASSSummary by CodeRabbit