Skip to content

SONARJAVA-6208 S2699: Add approve to assertion method name pattern#5570

Open
NoemieBenard wants to merge 6 commits intomasterfrom
nb/sonarjava-6208-assertions-in-tests
Open

SONARJAVA-6208 S2699: Add approve to assertion method name pattern#5570
NoemieBenard wants to merge 6 commits intomasterfrom
nb/sonarjava-6208-assertions-in-tests

Conversation

@NoemieBenard
Copy link
Copy Markdown

Add approve keyword to the assertion methods pattern to support ApproveJ library

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod bot commented Apr 14, 2026

SONARJAVA-6208

@sonar-review-alpha
Copy link
Copy Markdown

sonar-review-alpha bot commented Apr 14, 2026

Summary

This PR extends rule S2699 (test method contains assertions) to recognize ApproveJ's approve() method as a valid assertion. The change adds "approve" to the assertion method name pattern in UnitTestUtils.java, allowing tests that use ApproveJ for approval testing to pass the rule check instead of being flagged as having missing assertions.

The implementation includes:

  • Pattern update to match "approve" prefix (e.g., approve(), approveMessage())
  • Test cases demonstrating ApproveJ usage
  • Documentation update listing ApproveJ alongside other supported frameworks
  • Metrics baseline adjustment (158 false negatives, up from 157)

What reviewers should know

Start here: Review the pattern change in UnitTestUtils.java:44 — it's the only production code change. The regex now includes approve alongside existing assertion keywords.

Test coverage: The new ApproveJ.java file demonstrates both patterns — one method without assertions (expected to fail) and two using approve() (expected to pass).

Note: The false negatives metric increased by 1, which reflects a test case that was previously flagged but is now correctly handled by the enhanced pattern detection.


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

sonar-review-alpha[bot]

This comment was marked as resolved.

NoemieBenard and others added 2 commits April 14, 2026 13:07
Co-authored-by: sonar-review-alpha[bot] <266116024+sonar-review-alpha[bot]@users.noreply.github.com>
sonar-review-alpha[bot]

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Overall this change looks good, but we should also update the documentation in RSpec. Let me know when you have time and I'll show you how to do this.

sonar-review-alpha[bot]

This comment was marked as outdated.

<artifactId>core</artifactId>
<version>1.6.0</version>
<scope>test</scope>
</dependency>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please fix formatting.

@sonarqube-next
Copy link
Copy Markdown

Copy link
Copy Markdown

@sonar-review-alpha sonar-review-alpha bot left a comment

Choose a reason for hiding this comment

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

Small, focused PR overall. One question worth discussing regarding the autoscan metrics change.

🗣️ Give feedback

"hasTruePositives": true,
"falseNegatives": 157,
"falseNegatives": 158,
"falsePositives": 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The false negatives count went up by 1 (157 → 158) after adding approve to the pattern. This suggests the autoscan corpus contains at least one test method that calls something like approveOrder() or approvePayment() — a business-logic domain method that the rule now silently treats as an assertion, causing it to miss a genuine missing-assertion issue.

The word approve is broad enough to appear in non-assertion domain methods inside test code. Is this increase expected/acceptable as a deliberate trade-off, or is it a signal that the pattern should be narrowed (e.g. via a type-based check anchored to org.approvej, similar to how other frameworks are handled)?

  • Mark as noise

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.

2 participants