JUnitClassModifiers

SUGGESTION

Style

View source code on GitHub

Summary

Non-abstract JUnit test classes should be declared package-private and final

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("JUnitClassModifiers") to the enclosing element.

Disable this pattern completely by adding -Xep:JUnitClassModifiers:OFF as compiler argument. Learn more.

Samples

Replacement

Shows the difference in example code before and after the bug pattern is applied.

 import org.junit.jupiter.api.Test;
 import org.springframework.context.annotation.Configuration;
 
-public class A {
+final class A {
   @Test
   void foo() {}
 
   @Configuration
-  private static class B {
+  static class B {
     @Test
     void bar() {}
   }
 }
 

Identification

Shows code lines which will (not) be flagged by this bug pattern.
A //BUG: Diagnostic contains: comment is placed above any violating line.

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Configuration;

class Container {
  final class FinalAndPackagePrivate {
    @Test
    void foo() {}
  }

  final class FinalAndPackagePrivateWithCustomTestMethod {
    @ParameterizedTest
    void foo() {}
  }

  public abstract class Abstract {
    @Test
    void foo() {}
  }

  @Configuration
  class WithConfigurationAnnotation {
    @Test
    void foo() {}
  }

  @TestConfiguration
  class WithConfigurationMetaAnnotation {
    @Test
    void foo() {}
  }

  // BUG: Diagnostic contains:
  private final class Private {
    @Test
    void foo() {}
  }

  // BUG: Diagnostic contains:
  protected final class Protected {
    @Test
    void foo() {}
  }

  // BUG: Diagnostic contains:
  public final class Public {
    @Test
    void foo() {}
  }

  // BUG: Diagnostic contains:
  class NonFinal {
    @Test
    void foo() {}
  }

  // BUG: Diagnostic contains:
  class NonFinalWithCustomTestMethod {
    @ParameterizedTest
    void foo() {}
  }

  @Configuration
  // BUG: Diagnostic contains:
  public class PublicWithConfigurationAnnotation {
    @Test
    void foo() {}
  }

  @TestConfiguration
  // BUG: Diagnostic contains:
  protected class ProtectedWithConfigurationMetaAnnotation {
    @Test
    void foo() {}
  }
}

Copyright © 2017-2024 Picnic Technologies BV