Skip to content

[java] UnusedPrivateField: False positive on JUnit 5/6 @FieldSource #6606

@UncleOwen

Description

@UncleOwen

Affects PMD Version: 7.23.0

Rule: UnusedPrivateField

Description: When a private field is used as an argument source for a @ParameterizedTest, it is reported as unused by PMD.

Code Sample demonstrating the issue:

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.FieldSource;

import java.util.List;

class FooTest {
    private static final List<String> foo = List.of("a");

    @ParameterizedTest
    @FieldSource("foo")
    void testFoo(String arg) {}
}

Expected outcome:

PMD reports a violation at line 7, but that's wrong. That's a false positive.

Running PMD through: CLI

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions