Inspectopedia Help

JUnit malformed declaration

Reports JUnit test member declarations that are malformed and are likely not recognized by the JUnit test framework. The following problems are reported by this inspection:

  • Test classes that can't be constructed

  • Fields annotated by @RegisterExtension that have the wrong type or are not declared as static when it is required

  • Static or private inner classes annotated with @Nested

  • Parameterized tests that are defined without a source

  • Parameterized tests with a @MethodSource that has an unknown, non-static or no-arg target

  • Mismatched types between parameterized test method parameter and the specified @ValueSource or @EnumSource values

  • Tests that are annotated by more than one of @Test, @ParameterizedTest or @RepeatedTest

  • setup() or tearDown() methods that are not public, whose return type is not void or take arguments

  • suite() methods that are private, take arguments or are not static

  • Methods annotated by @BeforeClass, @AfterClass, @BeforeAll or @AfterAll that are not public, not static, whose return type is not void or do not have a valid parameter list

  • Methods annotated by @Before, @After, @BeforeEach or @AfterEach that are not public, whose return type is not void or take arguments

  • Injected RepetitionInfo in @BeforeAll or @AfterAll methods

  • Injected RepetitionInfo in @BeforeEach or @AfterEach methods that are used by @Test annotated tests

  • Fields and methods annotated by @DataPoint or @DataPoints that are not public or not static

  • Fields and methods annotated by @Rule that are not public or not a subtype of TestRule or MethodRule

  • Fields and methods annotated by @ClassRule that are not public, not static or not a subtype of TestRule

  • Methods inside a subclass of TestCase with a test prefix that are not public, whose return type is not void, take arguments or are static

  • Methods annotated by @Test that are not public, whose return type is not void, take arguments or are static

Note that in Kotlin, suspending functions do have arguments and a non-void return type. Therefore, they also will not be executed by the JUnit test runner. This inspection will also report about this problem.

Malformed @Before method example:

@Before private int foo(int arg) { }

After the quick-fix is applied:

@Before public void foo() { }

Missing method source example (Kotlin):

class Example { @MethodSource("parameters") @ParameterizedTest fun foo(param: String) { ... } }

After the quick-fix is applied:

class Example { @MethodSource("parameters") @ParameterizedTest fun foo(param: String) { ... } companion object { @JvmStatic fun parameters(): Stream<Arguments> { TODO("Not yet implemented") } } }

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

JUnitMalformedDeclaration
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | JUnit

Use the inspection options to specify annotations. Any parameter annotated with one of these annotations will not be reported.

Inspection options

Here you can find the description of settings available for the JUnit malformed declaration inspection, and the reference of their default values.

Ignore test parameter if it is annotated by

[mockit.Mocked, org.junit.jupiter.api.io.TempDir]

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.2, Qodana for JVM 2024.2,

Can be installed with plugin:

JUnit, 242.22892

Last modified: 11 September 2024