JUnit Parameterized No Data

ID

java.junit_parameterized_no_data

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

best-practice, junit, testing

Description

Reports classes annotated with @RunWith(Parameterized.class) that do not contain a static method annotated with @Parameters. The Parameterized runner requires a data provider method to supply test data. Without it, the runner throws an exception at startup.

Rationale

Reliability — The test class will fail to initialize and no tests will execute, giving a false impression that the test suite is complete.

Remediation

Non-compliant code

@RunWith(Parameterized.class)
public class MyTest {
    @Test public void testFoo() { }
    // missing @Parameters method
}

Compliant code

@RunWith(Parameterized.class)
public class MyTest {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] { {"a"}, {"b"} });
    }

    @Test public void testFoo() { }
}