Skip to main content Link Search Menu Expand Document (external link)

JUnitMethodDeclaration

SUGGESTION

Simplification

View source code on GitHub

Summary

JUnit method declaration can likely be improved

Suppression

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

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

Samples

Replacement

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

 import static org.junit.jupiter.params.provider.Arguments.arguments;
 
 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.RepeatedTest;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 
 class A {
   {
     arguments();
   }
 
   @BeforeAll
-  public void setUp1() {}
+  void setUp1() {}
 
   @BeforeEach
-  protected void setUp2() {}
+  void setUp2() {}
 
   @AfterEach
-  private void setUp3() {}
+  void setUp3() {}
 
   @AfterAll
-  private void setUp4() {}
+  void setUp4() {}
 
   @Test
-  void testFoo() {}
+  void foo() {}
 
   @ParameterizedTest
-  void testBar() {}
+  void bar() {}
 
   @Test
-  public void baz() {}
+  void baz() {}
 
   @RepeatedTest(2)
-  private void qux() {}
+  void qux() {}
 
   @ParameterizedTest
-  protected void quux() {}
+  void quux() {}
 
   @Test
-  public void testToString() {}
+  void testToString() {}
 
   @Test
-  public void testOverload() {}
+  void testOverload() {}
 
   void overload() {}
 
   @Test
-  protected void testArguments() {}
+  void testArguments() {}
 
   @Test
-  private void testClass() {}
+  void testClass() {}
 
   @Test
-  private void testTrue() {}
+  void testTrue() {}
 }

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.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

abstract class C {
  @BeforeAll
  public void setUp() {}

  @Test
  void testMethod1() {}

  @AfterAll
  // BUG: Diagnostic contains:
  private void tearDown() {}

  @Test
  // BUG: Diagnostic contains:
  final void testMethod2() {}
}
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.Test;
import org.junit.jupiter.params.ParameterizedTest;

class B extends A {
  @Override
  @BeforeAll
  void setUp1() {}

  @Override
  @BeforeAll
  public void setUp2() {}

  @Override
  @BeforeAll
  protected void setUp3() {}

  @Override
  @BeforeEach
  void setup5() {}

  @Override
  @BeforeEach
  public void setUp6() {}

  @Override
  @BeforeEach
  protected void setUp7() {}

  @Override
  @AfterEach
  void tearDown1() {}

  @Override
  @AfterEach
  public void tearDown2() {}

  @Override
  @AfterEach
  protected void tearDown3() {}

  @Override
  @AfterAll
  void tearDown5() {}

  @Override
  @AfterAll
  public void tearDown6() {}

  @Override
  @AfterAll
  protected void tearDown7() {}

  @Override
  @Test
  void test() {}

  @Override
  @Test
  void method1() {}

  @Override
  @Test
  void testMethod2() {}

  @Override
  @Test
  public void method3() {}

  @Override
  @Test
  protected void method4() {}

  @Override
  @ParameterizedTest
  void method6() {}

  @Override
  @ParameterizedTest
  void testMethod7() {}

  @Override
  @ParameterizedTest
  public void method8() {}

  @Override
  @ParameterizedTest
  protected void method9() {}

  @Override
  @BeforeEach
  @BeforeAll
  @AfterEach
  @AfterAll
  void testNonTestMethod1() {}

  @Override
  public void testNonTestMethod2() {}

  @Override
  protected void testNonTestMethod3() {}

  @Override
  @Test
  void test5() {}

  @Override
  @Test
  void testToString() {}

  @Override
  @Test
  void testOverload() {}

  @Override
  void overload() {}

  @Override
  @Test
  void testArguments() {}

  @Override
  @Test
  void testPublic() {}

  @Override
  @Test
  void testNull() {}

  @Override
  @Test
  void testRecord() {}

  @Test
  void testMethodThatIsOverriddenWithoutOverrideAnnotation() {}
}
import static org.junit.jupiter.params.provider.Arguments.arguments;

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.Test;
import org.junit.jupiter.params.ParameterizedTest;

class A {
  {
    arguments();
  }

  @BeforeAll
  void setUp1() {}

  @BeforeAll
  // BUG: Diagnostic contains:
  public void setUp2() {}

  @BeforeAll
  // BUG: Diagnostic contains:
  protected void setUp3() {}

  @BeforeAll
  // BUG: Diagnostic contains:
  private void setUp4() {}

  @BeforeEach
  void setup5() {}

  @BeforeEach
  // BUG: Diagnostic contains:
  public void setUp6() {}

  @BeforeEach
  // BUG: Diagnostic contains:
  protected void setUp7() {}

  @BeforeEach
  // BUG: Diagnostic contains:
  private void setUp8() {}

  @AfterEach
  void tearDown1() {}

  @AfterEach
  // BUG: Diagnostic contains:
  public void tearDown2() {}

  @AfterEach
  // BUG: Diagnostic contains:
  protected void tearDown3() {}

  @AfterEach
  // BUG: Diagnostic contains:
  private void tearDown4() {}

  @AfterAll
  void tearDown5() {}

  @AfterAll
  // BUG: Diagnostic contains:
  public void tearDown6() {}

  @AfterAll
  // BUG: Diagnostic contains:
  protected void tearDown7() {}

  @AfterAll
  // BUG: Diagnostic contains:
  private void tearDown8() {}

  @Test
  void test() {}

  @Test
  void method1() {}

  @Test
  // BUG: Diagnostic contains:
  void testMethod2() {}

  @Test
  // BUG: Diagnostic contains:
  public void method3() {}

  @Test
  // BUG: Diagnostic contains:
  protected void method4() {}

  @Test
  // BUG: Diagnostic contains:
  private void method5() {}

  @ParameterizedTest
  void method6() {}

  @ParameterizedTest
  // BUG: Diagnostic contains:
  void testMethod7() {}

  @ParameterizedTest
  // BUG: Diagnostic contains:
  public void method8() {}

  @ParameterizedTest
  // BUG: Diagnostic contains:
  protected void method9() {}

  @ParameterizedTest
  // BUG: Diagnostic contains:
  private void method10() {}

  @BeforeEach
  @BeforeAll
  @AfterEach
  @AfterAll
  void testNonTestMethod1() {}

  public void testNonTestMethod2() {}

  protected void testNonTestMethod3() {}

  private void testNonTestMethod4() {}

  @Test
  void test5() {}

  @Test
  // BUG: Diagnostic contains: (but note that a method named `toString` is already defined in this
  // class or a supertype)
  void testToString() {}

  @Test
  // BUG: Diagnostic contains: (but note that a method named `overload` is already defined in this
  // class or a supertype)
  void testOverload() {}

  void overload() {}

  @Test
  // BUG: Diagnostic contains: (but note that `arguments` is already statically imported)
  void testArguments() {}

  @Test
  // BUG: Diagnostic contains: (but note that `public` is not a valid identifier)
  void testPublic() {}

  @Test
  // BUG: Diagnostic contains: (but note that `null` is not a valid identifier)
  void testNull() {}

  @Test
  // BUG: Diagnostic contains:
  void testRecord() {}

  @Test
  // BUG: Diagnostic contains:
  void testMethodThatIsOverriddenWithoutOverrideAnnotation() {}
}

Copyright © 2017-2023 Picnic Technologies BV