Skip to content

Commit ec9cb36

Browse files
SONARJAVA-4944 S2699 should not raise on SpringBoot empty sanity test (#4777)
1 parent 4882bc0 commit ec9cb36

6 files changed

Lines changed: 88 additions & 3 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S2699",
33
"hasTruePositives": true,
4-
"falseNegatives": 147,
4+
"falseNegatives": 151,
55
"falsePositives": 1
6-
}
6+
}

java-checks-test-sources/default/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@
296296
<version>5.3.12</version>
297297
<scope>provided</scope>
298298
</dependency>
299+
<dependency>
300+
<groupId>org.springframework.boot</groupId>
301+
<artifactId>spring-boot-starter-test</artifactId>
302+
<version>3.2.4</version>
303+
<scope>provided</scope>
304+
</dependency>
299305
<dependency>
300306
<groupId>org.springframework</groupId>
301307
<artifactId>spring-jdbc</artifactId>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package checks.tests.AssertionsInTestsCheck;
2+
3+
import org.junit.Test;
4+
import org.junit.experimental.runners.Enclosed;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
8+
@RunWith(Enclosed.class)
9+
@SpringBootTest
10+
public class SpringBootSanityJ4Test {
11+
12+
@Test
13+
public void contextLoads() { // Compliant, no assertions needed for this spring sanity test
14+
}
15+
16+
@Test
17+
public void anotherTest(){ // Noncompliant, no assertions
18+
19+
}
20+
21+
public static class NotASpringBootSanityJ4Test {
22+
23+
@Test
24+
public void contextLoads() { // Noncompliant
25+
}
26+
27+
}
28+
29+
}
30+
31+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package checks.tests.AssertionsInTestsCheck;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class SpringBootSanityTest {
8+
9+
@Test
10+
void contextLoads() { // Compliant, no assertions needed for this spring sanity test
11+
}
12+
13+
@Test
14+
void anotherTest(){ // Noncompliant, no assertions
15+
16+
}
17+
18+
}
19+
20+
class NotASpringBootSanityTest {
21+
22+
@Test
23+
void contextLoads() { // Noncompliant
24+
}
25+
26+
}

java-checks/src/main/java/org/sonar/java/checks/tests/AssertionsInTestsCheck.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.sonar.plugins.java.api.semantic.Symbol;
3737
import org.sonar.plugins.java.api.semantic.SymbolMetadata;
3838
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
39+
import org.sonar.plugins.java.api.tree.ClassTree;
3940
import org.sonar.plugins.java.api.tree.MethodTree;
4041
import org.sonar.plugins.java.api.tree.Modifier;
4142
import org.sonar.plugins.java.api.tree.Tree;
@@ -77,11 +78,19 @@ public void visitMethod(MethodTree methodTree) {
7778
return;
7879
}
7980

80-
if (isUnitTest(methodTree) && !expectAssertion(methodTree) && !isLocalMethodWithAssertion(methodTree.symbol())) {
81+
if (isUnitTest(methodTree) && !isSpringBootSanityTest(methodTree) && !expectAssertion(methodTree) && !isLocalMethodWithAssertion(methodTree.symbol())) {
8182
context.reportIssue(this, methodTree.simpleName(), "Add at least one assertion to this test case.");
8283
}
8384
}
8485

86+
private static boolean isSpringBootSanityTest(MethodTree methodTree){
87+
if("contextLoads".equals(methodTree.simpleName().name())){
88+
ClassTree classTree = (ClassTree) methodTree.parent();
89+
return classTree.symbol().metadata().isAnnotatedWith("org.springframework.boot.test.context.SpringBootTest");
90+
}
91+
return false;
92+
}
93+
8594
private boolean isLocalMethodWithAssertion(Symbol symbol) {
8695
if (!assertionInMethod.containsKey(symbol)) {
8796
assertionInMethod.put(symbol, false);

java-checks/src/test/java/org/sonar/java/checks/tests/AssertionsInTestsCheckTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,17 @@ void testWithEmptyCustomAssertionMethods() {
107107
assertThat(logTester.logs(Level.WARN))
108108
.doesNotContain("Unable to create a corresponding matcher for custom assertion method, please check the format of the following symbol: ''");
109109
}
110+
111+
@Test
112+
void testSpringBootSanity(){
113+
CheckVerifier.newVerifier()
114+
.onFile(testCodeSourcesPath("checks/tests/AssertionsInTestsCheck/SpringBootSanityTestSample.java"))
115+
.withCheck(check)
116+
.verifyIssues();
117+
118+
CheckVerifier.newVerifier()
119+
.onFile(testCodeSourcesPath("checks/tests/AssertionsInTestsCheck/SpringBootSanityJ4Test.java"))
120+
.withCheck(check)
121+
.verifyIssues();
122+
}
110123
}

0 commit comments

Comments
 (0)