Skip to content

Commit

Permalink
SONARJAVA-4944 S2699 should not raise on SpringBoot empty sanity test (
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardo-pilastri-sonarsource authored Apr 19, 2024
1 parent 4882bc0 commit ec9cb36
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ruleKey": "S2699",
"hasTruePositives": true,
"falseNegatives": 147,
"falseNegatives": 151,
"falsePositives": 1
}
}
6 changes: 6 additions & 0 deletions java-checks-test-sources/default/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@
<version>5.3.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>3.2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package checks.tests.AssertionsInTestsCheck;

import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;

@RunWith(Enclosed.class)
@SpringBootTest
public class SpringBootSanityJ4Test {

@Test
public void contextLoads() { // Compliant, no assertions needed for this spring sanity test
}

@Test
public void anotherTest(){ // Noncompliant, no assertions

}

public static class NotASpringBootSanityJ4Test {

@Test
public void contextLoads() { // Noncompliant
}

}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package checks.tests.AssertionsInTestsCheck;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootSanityTest {

@Test
void contextLoads() { // Compliant, no assertions needed for this spring sanity test
}

@Test
void anotherTest(){ // Noncompliant, no assertions

}

}

class NotASpringBootSanityTest {

@Test
void contextLoads() { // Noncompliant
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.SymbolMetadata;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
import org.sonar.plugins.java.api.tree.ClassTree;
import org.sonar.plugins.java.api.tree.MethodTree;
import org.sonar.plugins.java.api.tree.Modifier;
import org.sonar.plugins.java.api.tree.Tree;
Expand Down Expand Up @@ -77,11 +78,19 @@ public void visitMethod(MethodTree methodTree) {
return;
}

if (isUnitTest(methodTree) && !expectAssertion(methodTree) && !isLocalMethodWithAssertion(methodTree.symbol())) {
if (isUnitTest(methodTree) && !isSpringBootSanityTest(methodTree) && !expectAssertion(methodTree) && !isLocalMethodWithAssertion(methodTree.symbol())) {
context.reportIssue(this, methodTree.simpleName(), "Add at least one assertion to this test case.");
}
}

private static boolean isSpringBootSanityTest(MethodTree methodTree){
if("contextLoads".equals(methodTree.simpleName().name())){
ClassTree classTree = (ClassTree) methodTree.parent();
return classTree.symbol().metadata().isAnnotatedWith("org.springframework.boot.test.context.SpringBootTest");
}
return false;
}

private boolean isLocalMethodWithAssertion(Symbol symbol) {
if (!assertionInMethod.containsKey(symbol)) {
assertionInMethod.put(symbol, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,17 @@ void testWithEmptyCustomAssertionMethods() {
assertThat(logTester.logs(Level.WARN))
.doesNotContain("Unable to create a corresponding matcher for custom assertion method, please check the format of the following symbol: ''");
}

@Test
void testSpringBootSanity(){
CheckVerifier.newVerifier()
.onFile(testCodeSourcesPath("checks/tests/AssertionsInTestsCheck/SpringBootSanityTestSample.java"))
.withCheck(check)
.verifyIssues();

CheckVerifier.newVerifier()
.onFile(testCodeSourcesPath("checks/tests/AssertionsInTestsCheck/SpringBootSanityJ4Test.java"))
.withCheck(check)
.verifyIssues();
}
}

0 comments on commit ec9cb36

Please sign in to comment.