Skip to content

Ensure assertions is contents aware - #3228

Merged
krmahadevan merged 1 commit into
testng-team:masterfrom
krmahadevan:fix/issue_3227
May 20, 2025
Merged

krmahadevan merged 1 commit into
testng-team:masterfrom
krmahadevan:fix/issue_3227

Conversation

@krmahadevan

@krmahadevan krmahadevan commented May 19, 2025

Copy link
Copy Markdown
Member

Closes #3227

Fixes #3227 .

Did you remember to?

  • Add test case(s)
  • Update CHANGES.txt
  • Auto applied styling via ./gradlew autostyleApply

We encourage pull requests that:

  • Add new features to TestNG (or)
  • Fix bugs in TestNG

If your pull request involves fixing SonarQube issues then we would suggest that you please discuss this with the
TestNG-dev before you spend time working on it.

Note: For more information on contribution guidelines please make sure you refer our Contributing section for detailed set of steps.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed an issue where collection equality assertions could incorrectly fail when the actual collection was a subset of the expected collection with the same size.
  • Tests

    • Added tests to ensure collection equality assertions accurately detect differences, including cases with duplicates.

@krmahadevan
krmahadevan requested a review from juherr as a code owner May 19, 2025 03:33
@coderabbitai

coderabbitai Bot commented May 19, 2025

Copy link
Copy Markdown

Walkthrough

The changes update the assertEqualsNoOrder method in the Assert class by introducing a private helper method that explicitly removes each expected element from a copy of the actual collection to correctly detect inequality. New tests for lists, queues, and iterators were added to verify the fix. The changelog was updated to document the resolution of issue GITHUB-3227.

Changes

File(s) Change Summary
CHANGES.txt Updated changelog to include fix for GITHUB-3227 related to false positives in assertEqualsNoOrder.
testng-asserts/src/main/java/org/testng/Assert.java Refactored assertEqualsNoOrder methods to delegate to a new private helper method performing element-wise removal checks for equality ignoring order.
testng-asserts/src/test/java/org/testng/AssertTest.java Added three new test methods with a shared data provider to verify the fix for lists, queues, and iterators.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Method
    participant Assert as Assert.assertEqualsNoOrder
    participant Helper as validateEqualityNoOrder

    Test->>Assert: assertEqualsNoOrder(actual, expected, message)
    Assert->>Helper: validateEqualityNoOrder(actual, expected, prefix, message)
    Helper->>Helper: Check size equality
    Helper->>Helper: Copy actual to list
    Helper->>Helper: For each element in expected, remove one occurrence from copy
    Helper->>Helper: If copy not empty, throw AssertionError with prefixed message
    Helper->>Test: Return if no error
Loading

Assessment against linked issues

Objective Addressed Explanation
Fix false positive in assertEqualsNoOrder when actual is a subset of expected but same size (#3227)

Poem

🐇 A hop, a skip, a fix so neat,
Collections now no longer cheat.
Each item checked, none left behind,
Order ignored, but truth we find.
No false alarms, just pure delight—
Assert equals done just right! 🌿✨

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 2fb4eaa and 5084c00.

📒 Files selected for processing (3)
  • CHANGES.txt (1 hunks)
  • testng-asserts/src/main/java/org/testng/Assert.java (2 hunks)
  • testng-asserts/src/test/java/org/testng/AssertTest.java (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • CHANGES.txt
  • testng-asserts/src/test/java/org/testng/AssertTest.java
  • testng-asserts/src/main/java/org/testng/Assert.java
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: 21, oracle, windows, UTC, ru_RU
  • GitHub Check: 24, oracle, windows, America/New_York, tr_TR
  • GitHub Check: 17, corretto, windows, America/New_York, fr_FR
  • GitHub Check: 21, oracle, macos, UTC, ru_RU, stress JIT
  • GitHub Check: 17, zulu, macos, America/New_York, fr_FR
  • GitHub Check: 11, microsoft, same hashcode, ubuntu, America/New_York, tr_TR, stress JIT
  • GitHub Check: 17, microsoft, macos, Pacific/Chatham, tr_TR
✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Comment thread testng-asserts/src/test/java/org/testng/AssertTest.java Outdated
@Test(description = "GITHUB-3227", expectedExceptions = AssertionError.class)
public void testAssertEqualsNoOrderCollection() {
var actual = List.of("a", "b", "b");
var expected = List.of("a", "b", "c");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace by a,a,b

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That change wont be a valid test because then both actual and expected will be equal (since the underlying set reduces it) and so no assertion failures will be triggered.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the point. Is it expected that aab and abb are equals?
And it could be extended: is it expected that aaaaaab is equals to abbbbbb?
In both case, I think it should be added as case in order to enforce the expectations

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the point. Is it expected that aab and abb are equals?

They are not supposed to be equal. But my implementation ends up making them equal. The problem is that both removeAll and Set dont solve the usecase. Looks like the only option is to be able to sort the collections and then try to compare it. I am not sure if the perf implications need to be considered here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can sort only if the assertion still holds after the initial check.
Otherwise, create a separate issue if you want to handle it later.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I relooked at this. I noticed that we have a assertEqualsNoOrder method for Object[] arrays and it resorts to removing one element at a time instead of doing a removeAll(). That way we ensure that we don't do a greedy removal but only 1 element at a time. Please check now

@krmahadevan
krmahadevan requested a review from juherr May 19, 2025 06:05
@krmahadevan
krmahadevan merged commit d50b2ad into testng-team:master May 20, 2025
@krmahadevan
krmahadevan deleted the fix/issue_3227 branch May 20, 2025 04:35
@juherr juherr added this to the 7.12.0 milestone Sep 7, 2026 — with ChatGPT Codex Connector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

assertEqualsNoOrder false positive when collection has same size and actual Collection is subset of expected collection

2 participants