JUnit Malformed Declaration

ID

java.junit_malformed_declaration

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

best-practice, junit, testing

Description

Reports @Test methods that violate JUnit’s declaration requirements: a private or static modifier, or a non-void return type on a JUnit 5 @Test. Such methods are silently ignored by the test runner and never execute.

Test method parameters are not reported. Parameters are how JUnit extensions hand a test what it needs, through an open-ended set of mechanisms — JUnit 5’s ParameterResolver (@TempDir, TestInfo, TestReporter, RepetitionInfo, Mockito @Mock parameters, Spring’s resolvers) and JUnit 4 custom runners such as DataProviderRunner — none of which a rule can enumerate.

Rationale

Reliability — The malformed test method will not be picked up by the test runner, giving a false sense of test coverage.

Remediation

Non-compliant code

@Test
private void testSomething() { }

@Test
static void testStatic() { }

@Test
String testSomethingElse() { return "result"; }

Compliant code

@Test
void testSomething() { }

@Test
void testSomethingElse() { }

// parameters are injected by the framework, and are never reported
@Test
void writesToDisk(@TempDir Path root) { }

© 2026 Xygeni. All rights reserved