Skip to content
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

SONARJAVA-5262 Fix FP on S2187 in Cucumber tests with JUnit5. #4972

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ruleKey": "S2187",
"hasTruePositives": true,
"falseNegatives": 13,
"falseNegatives": 14,
"falsePositives": 1
}
5 changes: 5 additions & 0 deletions java-checks-test-sources/default/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,11 @@
<artifactId>jspecify</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package checks.tests;

import org.junit.platform.suite.api.IncludeEngines;

@IncludeEngines("cucumber")
class NoTestInTestClassCheckCucumberStandardTest {}

@org.junit.platform.suite.api.IncludeEngines("cucumber")
class NoTestInTestClassCheckCucumberFullyQualifiedTest {}

@IncludeEngines("bellpepper")
class NoTestInTestClassCheckBellPepperTest {} // Noncompliant

@IncludeEngines({"spring", "cucumber"})
class NoTestInTestClassCheckTwoEnginesTest{}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ private void addUsedAnnotations(Symbol.TypeSymbol classSymbol) {
}

private static boolean runWithCucumberOrSuiteOrTheoriesRunner(Symbol.TypeSymbol symbol) {
return checkRunWith(symbol, "Cucumber", "Suite", "Theories");
return annotatedIncludeEnginesCucumber(symbol)
|| checkRunWith(symbol, "Cucumber", "Suite", "Theories");
}

private static boolean runWithZohhak(Symbol.TypeSymbol symbol) {
Expand Down Expand Up @@ -199,6 +200,28 @@ private static boolean checkRunWithType(Symbol.TypeSymbol value, String... runne
return false;
}

/**
* True if the symbol is annotated {@code @IncludeEngines("cucumber")}
* (with some approximation for automatic analysis).
*/
private static boolean annotatedIncludeEnginesCucumber(Symbol.TypeSymbol symbol) {
for (var annotation: symbol.metadata().annotations()) {
if (annotation.symbol().type().fullyQualifiedName().endsWith("IncludeEngines")) {
// values are not available in automatic analysis, so assume "cucumber" is there
if (annotation.values().isEmpty()) {
return true;
}
// otherwise check the list
boolean containsCucumber = annotation.values().stream().anyMatch(annotationValue ->
annotationValue.value() instanceof Object[] vals && Arrays.asList(vals).contains("cucumber"));
if (containsCucumber) {
return true;
}
}
}
return false;
}

private boolean isTestFieldOrMethod(Symbol member) {
return member.metadata().annotations().stream().anyMatch(input -> {
Type type = input.symbol().type();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,22 @@ void testNg() {
.withCheck(new NoTestInTestClassCheck())
.verifyIssues();
}

@Test
void testCucumber() {
CheckVerifier.newVerifier()
.onFile(testCodeSourcesPath("checks/tests/NoTestInTestClassCheckCucumberTest.java"))
.withCheck(new NoTestInTestClassCheck())
.verifyIssues();
}

@Test
void testCucumberWithoutSemantic() {
CheckVerifier.newVerifier()
.onFile(testCodeSourcesPath("checks/tests/NoTestInTestClassCheckCucumberTest.java"))
.withCheck(new NoTestInTestClassCheck())
.withoutSemantic()
// Note, that the sample file contains a noncompliant test, but we are fine with FN in automatic analysis.
.verifyNoIssues();
}
}
Loading