Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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
Comment on lines +1 to +15
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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 👍 / 👎

void setUp2() {
// pass
}


@Test
void decoy() {
fail("not important");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* SonarQube Java
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.checks.tests;

import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.ClassTree;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.MethodTree;
import org.sonar.plugins.java.api.tree.Tree;

import java.util.List;

// FIXME: get a rule id
@Rule(key = "S3360")
public class OneBeforeEachAfterEachCheck extends IssuableSubscriptionVisitor {

private static final String BEFORE_EACH = "org.junit.jupiter.api.BeforeEach";
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ 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 👍 / 👎

private static final String BEFORE = "org.junit.Before";

@Override
public List<Tree.Kind> nodesToVisit() {
return List.of(Tree.Kind.CLASS);
}

@Override
public void visitNode(Tree tree) {
ClassTree classTree = (ClassTree) tree;
IdentifierTree className = classTree.simpleName();
if (className == null) {
return;
}
int beforeCount = countAnnotations(BEFORE, classTree);
int beforeEachCount = countAnnotations(BEFORE_EACH, classTree);
if (beforeCount > 1 || beforeEachCount > 1) {
Comment thread
gitar-bot[bot] marked this conversation as resolved.
reportIssue(className, "Only one method should be annotated @Before(Each).");
}
}

private int countAnnotations(String annotation, ClassTree classTree) {
return (int) classTree.members().stream()
.filter(member -> member.is(Tree.Kind.METHOD))
.map(MethodTree.class::cast)
.filter(method -> method.symbol().metadata().isAnnotatedWith(annotation))
.count();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* SonarQube Java
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.checks.tests;

import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;

import static org.sonar.java.checks.verifier.TestUtils.testCodeSourcesPath;

class OneBeforeEachAfterEachCheckTest {
@Test
void test() {
CheckVerifier.newVerifier()
.onFile(testCodeSourcesPath("checks/tests/OneBeforeEachAfterEachCheckSample.java"))
.withCheck(new OneBeforeEachAfterEachCheck())
.verifyIssues();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h2>Why is this an issue?</h2>
<p>By default, the Maven Surefire plugin only executes test classes with names that end in "Test" or "TestCase". Name your class "TestClassX.java",
for instance, and it will be skipped.</p>
<p>This rule raises an issue for each test class with a name not ending in "Test" or "TestCase".</p>
<h3>Noncompliant code example</h3>
<pre>
public class TestClassX { // Noncompliant
@Test
public void testDoTheThing() {
//...
</pre>
<h3>Compliant solution</h3>
<pre>
public class ClassXTest {
@Test
public void testDoTheThing() {
//...
</pre>

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"title": "Test class names should end with \"Test\" or \"TestCase\"",
Comment on lines +1 to +2
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ 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 👍 / 👎

"type": "CODE_SMELL",
"code": {
"impacts": {
"MAINTAINABILITY": "BLOCKER"
},
"attribute": "IDENTIFIABLE"
},
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "2min"
},
"tags": [
"tests"
],
"defaultSeverity": "Blocker",
"ruleSpecification": "RSPEC-3360",
"sqKey": "S3360",
"scope": "Main",
"quickfix": "unknown"
}
Loading