SONARJAVA-6405#5636
Conversation
Agentic Analysis: Early ResultsAgentic Analysis and Context Augmentation are available on your project. Here are some issues that could have been prevented. Follow the links to learn how to put them into action. 4 issue(s) found across 2 file(s):
Analyzed by SonarQube Agentic Analysis in 5.4 s |
| @Rule(key = "S3360") | ||
| public class OneBeforeEachAfterEachCheck extends IssuableSubscriptionVisitor { | ||
|
|
||
| private static final String BEFORE_EACH = "org.junit.jupiter.api.BeforeEach"; |
There was a problem hiding this comment.
⚠️ Bug: Rule only checks @beforeeach, ignores @AfterEach
The class is named OneBeforeEachAfterEachCheck and the rule intent (per the ticket title) is to check both @BeforeEach and @AfterEach, but the implementation only defines and checks BEFORE_EACH. A corresponding AFTER_EACH = "org.junit.jupiter.api.AfterEach" constant and check is missing.
Add the @AfterEach check alongside the existing @beforeeach check:
private static final String BEFORE_EACH = "org.junit.jupiter.api.BeforeEach";
private static final String AFTER_EACH = "org.junit.jupiter.api.AfterEach";
// ... in visitNode, after the existing beforeEachCount check:
long afterEachCount = classTree.members().stream()
.filter(member -> member.is(Tree.Kind.METHOD))
.map(MethodTree.class::cast)
.filter(method -> method.symbol().metadata().isAnnotatedWith(AFTER_EACH))
.count();
if (afterEachCount > 1) {
reportIssue(className, "Only one method should be annotated @AfterEach.");
}
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
| { | ||
| "title": "Test class names should end with \"Test\" or \"TestCase\"", |
There was a problem hiding this comment.
⚠️ Bug: S3360 rule metadata describes a completely different rule
The S3360.json title is "Test class names should end with 'Test' or 'TestCase'" and S3360.html documents a naming convention rule. These are clearly copied from another rule and do not match the @BeforeEach/@AfterEach check being implemented. This will surface incorrect documentation to users. The FIXME at line 28 of the check also indicates the rule key is provisional.
Was this helpful? React with 👍 / 👎
27e554c to
156e7d9
Compare
| package checks.tests; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| class OneBeforeEachAfterEachCheckSample { // Noncompliant {{Only one method should be annotated @Before(Each).}} | ||
|
|
||
| @BeforeEach | ||
| void setUp1() { | ||
| // pass | ||
| } | ||
|
|
||
| @BeforeEach |
There was a problem hiding this comment.
💡 Quality: Test sample only covers @beforeeach, not @before or @after*
The test sample OneBeforeEachAfterEachCheckSample.java only tests the case of multiple @BeforeEach methods. It doesn't test @Before, @AfterEach, @After, compliant cases (single annotation), or mixed annotation scenarios. This leaves most of the rule's intended behavior unverified.
Was this helpful? React with 👍 / 👎
Code Review
|
| Auto-apply | Compact | Unblock |
|
|
|
Was this helpful? React with 👍 / 👎 | Gitar
Summary by Gitar
OneBeforeEachAfterEachCheckto identify test classes containing multiple methods annotated with@BeforeEachor@Before.OneBeforeEachAfterEachCheckTestandOneBeforeEachAfterEachCheckSampleto verify the rule functionality.S3360.jsonandS3360.html(Note: current metadata content currently contains placeholder text unrelated to this rule).This will update automatically on new commits.