JUnitNullaryParameterizedTestDeclaration

SUGGESTION

Simplification

View source code on GitHub

Summary

Nullary JUnit test methods should not be parameterized

Suppression

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

Disable this pattern completely by adding -Xep:JUnitNullaryParameterizedTestDeclaration: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.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.ArgumentsProvider;
 import org.junit.jupiter.params.provider.ArgumentsSource;
 import org.junit.jupiter.params.provider.ArgumentsSources;
 import org.junit.jupiter.params.provider.MethodSource;
 import org.junit.jupiter.params.provider.ValueSource;
 
 class A {
-  @ParameterizedTest
+  @Test
   void withoutArgumentSource() {}
 
-  @ParameterizedTest
-  @ArgumentsSource(ArgumentsProvider.class)
+  @Test
   void withCustomArgumentSource() {}
 
-  @ParameterizedTest
-  @ArgumentsSources({
-    @ArgumentsSource(ArgumentsProvider.class),
-    @ArgumentsSource(ArgumentsProvider.class)
-  })
+  @Test
   void withCustomerArgumentSources() {}
 
   /** Foo. */
-  @ParameterizedTest
-  @ValueSource(ints = {0, 1})
+  @Test
   void withValueSourceAndJavadoc() {}
 
-  @ParameterizedTest
-  @MethodSource("nonexistentMethod")
+  @Test
   @SuppressWarnings("foo")
   void withMethodSourceAndUnrelatedAnnotation() {}
 
-  @org.junit.jupiter.params.ParameterizedTest
-  @ArgumentsSource(ArgumentsProvider.class)
-  @ValueSource(ints = {0, 1})
-  @MethodSource("nonexistentMethod")
+  @Test
   void withMultipleArgumentSourcesAndFullyQualifiedImport() {}
 
   class NestedWithTestAnnotationFirst {
-    @ParameterizedTest
-    @ValueSource(ints = {0, 1})
+    @Test
     void withValueSource() {}
   }
 
   class NestedWithTestAnnotationSecond {
-    @ValueSource(ints = {0, 1})
-    @ParameterizedTest
+    @Test
     void withValueSource() {}
   }
 }
 
 import org.junit.jupiter.params.ParameterizedTest;
 
 class B {
-  @ParameterizedTest
+  @org.junit.jupiter.api.Test
   void scopeInWhichIdentifierTestIsAlreadyDeclared() {}
 
   class Test {}
 }
 

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.junit.jupiter.params.provider.ValueSource;

class A {
  void nonTest() {}

  @Test
  void nonParameterizedTest() {}

  @ParameterizedTest
  @ValueSource(ints = {0, 1})
  void goodParameterizedTest(int someInt) {}

  @ParameterizedTest
  @ValueSource(ints = {0, 1})
  // BUG: Diagnostic contains:
  void nullaryParameterizedTest() {}
}

Copyright © 2017-2024 Picnic Technologies BV