SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • ShellShell
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
Java

Java static code analysis

Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your JAVA code

  • All rules 733
  • Vulnerability60
  • Bug175
  • Security Hotspot40
  • Code Smell458

  • Quick Fix 65
Filtered: 21 rules found
junit
    Impact
      Clean code attribute
        1. Methods setUp() and tearDown() should be correctly annotated starting with JUnit4

           Code Smell
        2. JUnit5 test classes and methods should not be silently ignored

           Bug
        3. Migrate your tests from JUnit4 to the new JUnit5 annotations

           Code Smell
        4. JUnit5 inner test classes should be annotated with @Nested

           Bug
        5. JUnit5 test classes and methods should have default package visibility

           Code Smell
        6. JUnit assertTrue/assertFalse should be simplified to the corresponding dedicated assertion

           Code Smell
        7. Only one method invocation is expected when testing checked exceptions

           Bug
        8. Assertion methods should not be used within the try block of a try-catch catching an Error

           Bug
        9. Only one method invocation is expected when testing runtime exceptions

           Code Smell
        10. Exception testing via JUnit @Test annotation should be avoided

           Code Smell
        11. Exception testing via JUnit ExpectedException rule should not be mixed with other assertions

           Code Smell
        12. Unit tests should throw exceptions

           Code Smell
        13. Assertion arguments should be passed in the correct order

           Code Smell
        14. JUnit rules should be used

           Code Smell
        15. Literal boolean values and nulls should not be used in assertions

           Code Smell
        16. Tests should include assertions

           Code Smell
        17. Test assertions should include messages

           Code Smell
        18. JUnit test cases should call super methods

           Code Smell
        19. TestCases should contain tests

           Code Smell
        20. JUnit assertions should not be used in "run" methods

           Code Smell
        21. JUnit4 @Ignored and JUnit5 @Disabled annotations should be used to disable tests and should provide a rationale

           Code Smell

        Migrate your tests from JUnit4 to the new JUnit5 annotations

        consistency - conventional
        maintainability
        Code Smell
        • junit
        • tests

        Why is this an issue?

        More Info

        As mentioned in JUnit5 documentation, it is possible to integrate JUnit4 with JUnit5:

        JUnit provides a gentle migration path via a JUnit Vintage test engine which allows existing tests based on JUnit 3 and JUnit 4 to be executed using the JUnit Platform infrastructure. Since all classes and annotations specific to JUnit Jupiter reside under a new org.junit.jupiter base package, having both JUnit 4 and JUnit Jupiter in the classpath does not lead to any conflicts.

        However, maintaining both systems is a temporary solution. This rule flags all the annotations from JUnit4 which would need to be migrated to JUnit5, hence helping migration of a project.

        Here is the list of JUnit4 annotations tracked by the rule, with their corresponding annotations in JUnit5:

        JUnit4 JUnit5

        org.junit.Test

        org.junit.jupiter.api.Test

        org.junit.Before

        org.junit.jupiter.api.BeforeEach

        org.junit.After

        org.junit.jupiter.api.AfterEach

        org.junit.BeforeClass

        org.junit.jupiter.api.BeforeAll

        org.junit.AfterClass

        org.junit.jupiter.api.AfterAll

        org.junit.Ignore

        org.junit.jupiter.api.Disabled

        Note that the following annotations might requires some rework of the tests to have JUnit5 equivalent behavior. A simple replacement of the annotation won’t work immediately:

        JUnit4 JUnit5

        org.junit.experimental.categories.Category

        org.junit.jupiter.api.Tag

        org.junit.Rule

        org.junit.jupiter.api.extension.ExtendWith

        org.junit.ClassRule

        org.junit.jupiter.api.extension.RegisterExtension

        org.junit.runner.RunWith

        org.junit.jupiter.api.extension.ExtendWith

        Noncompliant code example

        package org.foo;
        
        import org.junit.After;
        import org.junit.AfterClass;
        import org.junit.Before;
        import org.junit.BeforeClass;
        import org.junit.Ignore;
        import org.junit.Test;
        import org.junit.experimental.categories.Category;
        import org.junit.runner.RunWith;
        
        @RunWith(MyJUnit4Runner.class)
        public class MyJUnit4Test {
        
          @BeforeClass
          public static void beforeAll() {
            System.out.println("beforeAll");
          }
        
          @AfterClass
          public static void afterAll() {
            System.out.println("AfterAll");
          }
        
          @Before
          public void beforeEach() {
            System.out.println("beforeEach");
          }
        
          @After
          public void afterEach() {
            System.out.println("afterEach");
          }
        
          @Test
          public void test1() throws Exception {
            System.out.println("test1");
          }
        
          public interface SomeTests { /* category marker */ }
        
          @Test
          @Category(SomeTests.class)
          public void test2() throws Exception {
            System.out.println("test2");
          }
        
          @Test
          @Ignore("Requires fix of #42")
          public void ignored() throws Exception {
            System.out.println("ignored");
          }
        }
        

        Compliant solution

        package org.foo;
        
        import org.junit.jupiter.api.AfterAll;
        import org.junit.jupiter.api.AfterEach;
        import org.junit.jupiter.api.BeforeAll;
        import org.junit.jupiter.api.BeforeEach;
        import org.junit.jupiter.api.Disabled;
        import org.junit.jupiter.api.Tag;
        import org.junit.jupiter.api.Test;
        import org.junit.jupiter.api.extension.ExtendWith;
        
        @ExtendWith(MyJUnit5Extension.class)
        class MyJUnit5Test {
        
          @BeforeAll
          static void beforeAll() {
            System.out.println("beforeAll");
          }
        
          @AfterAll
          static void afterAll() {
            System.out.println("afterAll");
          }
        
          @BeforeEach
          void beforeEach() {
            System.out.println("beforeEach");
          }
        
          @AfterEach
          void afterEach() {
            System.out.println("afterEach");
          }
        
          @Test
          void test1() {
            System.out.println("test1");
          }
        
          @Test
          @Tag("SomeTests")
          void test2() {
            System.out.println("test2");
          }
        
          @Test
          @Disabled("Requires fix of #42")
          void disabled() {
            System.out.println("ignored");
          }
        }
        
          Available In:
        • SonarQube IdeCatch issues on the fly,
          in your IDE
        • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories
        • SonarQube Community BuildAnalyze code in your
          on-premise CI
          Available Since
          9.1
        • SonarQube ServerAnalyze code in your
          on-premise CI
          Developer Edition
          Available Since
          9.1

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use