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 requiredStatic 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 targetMismatched types between parameterized test method parameter and the specified
@ValueSource
or@EnumSource
valuesTests that are annotated by more than one of
@Test
,@ParameterizedTest
or@RepeatedTest
setup()
ortearDown()
methods that are not public, whose return type is not void or take argumentssuite()
methods that are private, take arguments or are not staticMethods 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 listMethods annotated by
@Before
,@After
,@BeforeEach
or@AfterEach
that are not public, whose return type is not void or take argumentsInjected
RepetitionInfo
in@BeforeAll
or@AfterAll
methodsInjected
RepetitionInfo
in@BeforeEach
or@AfterEach
methods that are used by@Test
annotated testsFields and methods annotated by
@DataPoint
or@DataPoints
that are not public or not staticFields and methods annotated by
@Rule
that are not public or not a subtype ofTestRule
orMethodRule
Fields and methods annotated by
@ClassRule
that are not public, not static or not a subtype ofTestRule
Methods inside a subclass of
TestCase
with atest
prefix that are not public, whose return type is not void, take arguments or are staticMethods 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 (Java):
@Before private int foo(int arg) { ... }
After the quick-fix is applied:
@Before public void foo() { ... }
Missing method source example (Kotlin):
After the quick-fix is applied:
Use the inspection options to specify annotations. Any parameter annotated with one of these annotations will not be reported.
Inspection options
Option | Type | Default |
---|---|---|
Ignore test parameter if it is annotated by | StringList | [mockit.Mocked, org.junit.jupiter.api.io.TempDir] |
Inspection Details | |
---|---|
Available in: | IntelliJ IDEA 2023.3, Qodana for JVM 2023.3 |
Plugin: | Java, 233.SNAPSHOT |