SONARJAVA-6423 Implement S8715 No JUnit 4 assertions in JUnit 5 tests#5645
SONARJAVA-6423 Implement S8715 No JUnit 4 assertions in JUnit 5 tests#5645tomasz-tylenda-sonarsource wants to merge 2 commits into
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. 2 issue(s) found across 1 file(s):
Analyzed by SonarQube Agentic Analysis in 11.6 s |
There was a problem hiding this comment.
Comment gitar unblock to override this block and allow merging.
Configure merge blocking · Maintainers can dismiss this review. Gitar never approves changes.
2e27501 to
72c2b4b
Compare
d23522b to
9ce846c
Compare
| @Test | ||
| void test() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(testCodeSourcesPath("checks/tests/JUnit4AssertionsCheckSampleTest.java")) |
There was a problem hiding this comment.
🚨 Bug: Test file name case mismatch will cause test failure on Linux
The test references checks/tests/JUnit4AssertionsCheckSampleTest.java (capital 'U' in JUnit) but the actual sample file is named Junit4AssertionsCheckSampleTest.java (lowercase 'u'). On case-sensitive file systems (Linux CI), this will cause the test to fail with a FileNotFoundException.
Fix 1: Fix the path in the test to match the actual file name (lowercase 'u' in Junit)
.onFile(testCodeSourcesPath("checks/tests/Junit4AssertionsCheckSampleTest.java"))
- Apply fix
Fix 2: Alternatively, rename the sample file to match the test's expected path (capital 'U' in JUnit), which is more consistent with the check class name JUnit4AssertionsCheck
Rename the sample file from Junit4AssertionsCheckSampleTest.java to JUnit4AssertionsCheckSampleTest.java
- Apply fix
Check a box to apply a fix or reply for a change | Was this helpful? React with 👍 / 👎
| public class JUnit4AssertionsCheck extends IssuableSubscriptionVisitor { | ||
| private static final String MESSAGE = "JUnit Jupiter tests should not use JUnit 4 assertions."; | ||
|
|
||
| private static final String JUPITER_TEST_ANNOTATION = "org.junit.jupiter.api.Test"; |
There was a problem hiding this comment.
⚠️ Bug: Check only detects @test but misses other Jupiter annotations
The check only considers methods annotated with org.junit.jupiter.api.Test, but JUnit 5 test methods can also be annotated with @ParameterizedTest, @RepeatedTest, @TestFactory, or @TestTemplate. The codebase already provides UnitTestUtils.hasJUnit5TestAnnotation(MethodTree) which covers all five annotations. Using JUnit 4 assertions in a @ParameterizedTest method would not be flagged by this check.
Other checks in the codebase (e.g., AbstractJUnit5NotCompliantModifierChecker) use UnitTestUtils.hasJUnit5TestAnnotation() for this purpose.
Use the existing UnitTestUtils.hasJUnit5TestAnnotation() helper to cover all Jupiter test annotations (@test, @ParameterizedTest, @RepeatedTest, @testfactory, @testtemplate):
private static boolean isInJupiterTest(MethodInvocationTree mit) {
MethodTree enclosingMethod = ExpressionUtils.getEnclosingMethod(mit);
return enclosingMethod != null && UnitTestUtils.hasJUnit5TestAnnotation(enclosingMethod);
}
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
Code Review 🚫 Blocked 3 resolved / 5 findingsImplements S8715 to identify JUnit 4 assertions within JUnit 5 tests, but the check currently fails to account for all Jupiter annotations and contains a file naming case mismatch that will break Linux builds. 🚨 Bug: Test file name case mismatch will cause test failure on Linux📄 java-checks/src/test/java/org/sonar/java/checks/tests/JUnit4AssertionsCheckTest.java:28 📄 java-checks-test-sources/default/src/test/java/checks/tests/Junit4AssertionsCheckSampleTest.java:1 The test references Fix the path in the test to match the actual file name (lowercase 'u' in Junit)Alternatively, rename the sample file to match the test's expected path (capital 'U' in JUnit), which is more consistent with the check class name JUnit4AssertionsCheck
|
| Auto-apply | Compact | Unblock |
|
|
|
Was this helpful? React with 👍 / 👎 | Gitar
Summary by Gitar
JUnit4AssertionsCheckto flag usage of JUnit 4 assertions (org.junit.Assert) in JUnit Jupiter tests.S8715.html), configuration (S8715.json), and integrated the rule intoSonar agentic AIandSonar wayprofiles.JUnit4AssertionsCheckTestand sample code to verify detection, and updatedJavaAgenticWayProfileTestto account for the new rule.diff_S8715.jsonto the autoscan test resources.This will update automatically on new commits.