-
Notifications
You must be signed in to change notification settings - Fork 725
SONARJAVA-6208 S2699: Add approve to assertion method name pattern #5570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
004cfe3
add assertion detection for ApproveJ testing library
NoemieBenard ac0b218
add ApproveJ dependency
NoemieBenard c8fc8e3
Update java-checks-test-sources/default/pom.xml
NoemieBenard e0e7d19
fix pom.xml after removing dependency
NoemieBenard 68c5e8f
fix autoscan
NoemieBenard 58da571
modify rule S2699 documentation
NoemieBenard 87b18eb
fix formatting
NoemieBenard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "ruleKey": "S2699", | ||
| "hasTruePositives": true, | ||
| "falseNegatives": 157, | ||
| "falseNegatives": 158, | ||
| "falsePositives": 1 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...ecks-test-sources/default/src/test/java/checks/tests/AssertionsInTestsCheck/ApproveJ.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package checks.tests.AssertionsInTestsCheck; | ||
|
|
||
| import java.time.LocalDate; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.approvej.ApprovalBuilder.approve; | ||
| import static org.approvej.print.MultiLineStringPrintFormat.multiLineString; | ||
|
|
||
| class ApproveJTest { | ||
|
|
||
| @Test | ||
| void contains_no_assertions() { // Noncompliant | ||
| } | ||
|
|
||
| public String hello() {return "hello";} | ||
|
|
||
| @Test | ||
| void approve_string() { | ||
| String result = hello(); | ||
| approve(result) | ||
| .byFile(); | ||
| } | ||
|
|
||
| public record Person(String name, LocalDate birthDate) {} | ||
|
|
||
| @Test | ||
| void approve_person() { | ||
| Person jane = new Person("Jane Doe", LocalDate.of(1990, 1, 1)); | ||
| approve(jane).named("jane").printedAs(multiLineString()).byFile(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
approveto the pattern. This suggests the autoscan corpus contains at least one test method that calls something likeapproveOrder()orapprovePayment()— 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
approveis 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 toorg.approvej, similar to how other frameworks are handled)?