-
Notifications
You must be signed in to change notification settings - Fork 727
SONARJAVA-6423 Implement S8715 No JUnit 4 assertions in JUnit 5 tests #5645
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "ruleKey": "S8715", | ||
| "hasTruePositives": false, | ||
| "falseNegatives": 165, | ||
| "falsePositives": 0 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package checks.tests; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class Junit4AssertionsCheckSampleTest { | ||
| @Test | ||
| void good() { | ||
| org.junit.jupiter.api.Assertions.assertEquals(2, 1 + 1); | ||
| } | ||
|
|
||
| @Test | ||
| void bad() { | ||
| org.junit.Assert.assertEquals(2, 1 + 1); // Noncompliant {{JUnit Jupiter tests should not use JUnit 4 assertions.}} | ||
| // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| } | ||
|
|
||
| @org.junit.Test | ||
| public void testOld() { | ||
| org.junit.Assert.assertEquals(2, 1 + 1); // compliant | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * 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 java.util.List; | ||
| import org.sonar.check.Rule; | ||
| import org.sonar.java.model.ExpressionUtils; | ||
| import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
| import org.sonar.plugins.java.api.semantic.MethodMatchers; | ||
| import org.sonar.plugins.java.api.tree.MethodInvocationTree; | ||
| import org.sonar.plugins.java.api.tree.MethodTree; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
|
|
||
| /** | ||
| * Check that JUnit Jupiter (JUnit 5) tests do not use JUnit 4 assertions. | ||
| */ | ||
| @Rule(key = "S8715") | ||
| 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"; | ||
|
|
||
| private static final MethodMatchers JUNIT4_ASSERT = MethodMatchers.create() | ||
| .ofTypes("org.junit.Assert") | ||
| .anyName() | ||
| .withAnyParameters() | ||
| .build(); | ||
|
|
||
| @Override | ||
| public List<Tree.Kind> nodesToVisit() { | ||
| return List.of(Tree.Kind.METHOD_INVOCATION); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitNode(Tree tree) { | ||
| if (tree instanceof MethodInvocationTree mit && JUNIT4_ASSERT.matches(mit) && isInJupiterTest(mit)) { | ||
| reportIssue(mit.methodSelect(), MESSAGE); | ||
| } | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
| } | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
|
|
||
| private static boolean isInJupiterTest(MethodInvocationTree mit) { | ||
| MethodTree enclosingMethod = ExpressionUtils.getEnclosingMethod(mit); | ||
| return enclosingMethod != null && enclosingMethod.symbol().metadata().isAnnotatedWith(JUPITER_TEST_ANNOTATION); | ||
| } | ||
| } | ||
| 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 JUnit4AssertionsCheckTest { | ||
| @Test | ||
| void test() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(testCodeSourcesPath("checks/tests/JUnit4AssertionsCheckSampleTest.java")) | ||
|
gitar-bot[bot] marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚨 Bug: Test file name case mismatch will cause test failure on LinuxThe test references Fix 1: Fix the path in the test to match the actual file name (lowercase 'u' in Junit)
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
Check a box to apply a fix or reply for a change | Was this helpful? React with 👍 / 👎 |
||
| .withCheck(new JUnit4AssertionsCheck()) | ||
| .verifyIssues(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <h2>Why is this an issue?</h2> | ||
| <p>This rule targets tests written with the JUnit Jupiter API (JUnit 5 and later), i.e. annotated with <code>org.junit.jupiter.api.Test</code>. While | ||
| JUnit 4 assertions defined in <code>org.junit.Assert</code> continue to work in such tests, using them should be avoided.</p> | ||
| <p>JUnit 5 ships with its own assertion library, <code>org.junit.jupiter.api.Assertions</code>, which is richer and consistent with the rest of the | ||
| JUnit 5 API. Using JUnit 4 assertions in a JUnit 5 test is inconsistent. Because the two APIs differ in subtle ways, it can also lead to confusion and | ||
| errors:</p> | ||
| <ul> | ||
| <li>The position of the optional failure message argument is reversed. <code>org.junit.Assert</code> takes the message as the <strong>first</strong> | ||
| argument, <code>org.junit.jupiter.api.Assertions</code> as the <strong>last</strong>.</li> | ||
| <li>JUnit 5 overloads accept a <code>Supplier<String></code>, so the failure message can be built lazily, which is not possible with JUnit | ||
| 4.</li> | ||
| </ul> | ||
| <h2>How to fix it</h2> | ||
| <p>Replace each call to a method of <code>org.junit.Assert</code> with the equivalent method of <code>org.junit.jupiter.api.Assertions</code>. If a | ||
| failure message is provided, move it from the first to the last argument position.</p> | ||
| <h3>Code examples</h3> | ||
| <h4>Noncompliant code example</h4> | ||
| <pre data-diff-id="1" data-diff-type="noncompliant"> | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| class MyTest { | ||
| @Test | ||
| void shouldComputeAnswer() { | ||
| assertEquals("values should match", 42, compute()); // Noncompliant | ||
| } | ||
| } | ||
| </pre> | ||
| <h4>Compliant solution</h4> | ||
| <pre data-diff-id="1" data-diff-type="compliant"> | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| class MyTest { | ||
| @Test | ||
| void shouldComputeAnswer() { | ||
| assertEquals(42, compute(), "values should match"); | ||
| } | ||
| } | ||
| </pre> | ||
| <h3>Going the extra mile</h3> | ||
| <p>If you find the JUnit 5 assertion library limited, consider using a dedicated assertion library such as <a | ||
| href="https://assertj.github.io/doc/">AssertJ</a> or <a href="https://hamcrest.org/JavaHamcrest/">Hamcrest</a>.</p> | ||
| <h2>Resources</h2> | ||
| <h3>Documentation</h3> | ||
| <ul> | ||
| <li>JUnit 5 - <a href="https://docs.junit.org/6.1.0/writing-tests/assertions.html">Assertions</a></li> | ||
| <li>JUnit 5 - <a href="https://docs.junit.org/6.1.0/migrating-from-junit4.html#tips">Migration Tips</a></li> | ||
| </ul> | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "title": "JUnit Jupiter tests should not use JUnit 4 assertions", | ||
| "type": "CODE_SMELL", | ||
| "status": "ready", | ||
| "remediation": { | ||
| "func": "Constant\/Issue", | ||
| "constantCost": "5min" | ||
| }, | ||
| "tags": [ | ||
| "junit", | ||
| "tests" | ||
| ], | ||
| "defaultSeverity": "Minor", | ||
| "ruleSpecification": "RSPEC-8715", | ||
| "sqKey": "S8715", | ||
| "scope": "Tests", | ||
| "quickfix": "targeted", | ||
| "code": { | ||
| "impacts": { | ||
| "MAINTAINABILITY": "LOW" | ||
| }, | ||
| "attribute": "CONVENTIONAL" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -468,6 +468,7 @@ | |
| "S8450", | ||
| "S8465", | ||
| "S8469", | ||
| "S8696" | ||
| "S8696", | ||
| "S8715" | ||
| ] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -536,6 +536,7 @@ | |
| "S8694", | ||
| "S8695", | ||
| "S8696", | ||
| "S8700" | ||
| "S8700", | ||
| "S8715" | ||
| ] | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 providesUnitTestUtils.hasJUnit5TestAnnotation(MethodTree)which covers all five annotations. Using JUnit 4 assertions in a@ParameterizedTestmethod would not be flagged by this check.Other checks in the codebase (e.g.,
AbstractJUnit5NotCompliantModifierChecker) useUnitTestUtils.hasJUnit5TestAnnotation()for this purpose.Use the existing UnitTestUtils.hasJUnit5TestAnnotation() helper to cover all Jupiter test annotations (@test, @ParameterizedTest, @RepeatedTest, @testfactory, @testtemplate):
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎