Skip to content

[java] UnusedPrivateMethod / UnusedPrivateField: False positives for fully qualified @MethodSource / @FieldSource references #7213

Closed

Description

@Chordrain

Affects PMD Version: 7.24.0

Rule: UnusedPrivateMethod — https://docs.pmd-code.org/latest/pmd_rules_java_bestpractices.html#unusedprivatemethod

Description:

The UnusedPrivateMethod rule correctly recognises private factory methods referenced from JUnit 5's @MethodSource when the simple form (@MethodSource("foo")) is used. However, when the same factory method is referenced through the equivalent fully-qualified form documented by JUnit (@MethodSource("fqcn#methodName")), PMD fails to follow the reference and reports the private method as unused.

This is a false positive: per the JUnit 5 docs for @MethodSource, the value may be a simple method name or a fully-qualified fully.qualified.ClassName#methodName reference, and both resolve to the same factory method.

Code Sample demonstrating the issue:

package demo.after;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.api.Assertions;

public class StringUtilsTest {

    // Used as the factory method for testTrimAndUpperCase below.
    private static Stream<Arguments> trimAndUpperCaseArguments() {
        return Stream.of(
                Arguments.of("foo"),
                Arguments.of("bar"),
                Arguments.of("baz")
        );
    }

    // Fully-qualified form — PMD does NOT recognise this as a usage.
    @MethodSource("demo.after.StringUtilsTest#trimAndUpperCaseArguments")
    @ParameterizedTest
    void testTrimAndUpperCase(String input) {
        Assertions.assertNotNull(input);
    }
}

PMD output:

StringUtilsTest.java:13:	UnusedPrivateMethod:	Avoid unused private methods such as 'trimAndUpperCaseArguments()'.

Replacing the annotation with the simple form @MethodSource("trimAndUpperCaseArguments") makes the warning disappear, although both forms refer to the same method.

Expected outcome: PMD should treat the fully-qualified Class#method form in @MethodSource the same way as the simple form and not flag the referenced private static method as unused.

Running PMD through: CLI

Activity

  1. added
    a:false-positive
    on Oct 21, 2026
  2. changed the title [-][java] UnusedPrivateMethod: false positive when @MethodSource uses a fully-qualified Class#method reference[/-] [+][java] UnusedPrivateMethod / UnusedPrivateField: false positives for fully-qualified @MethodSource / @FieldSource references[/+] on Oct 29, 2026
  3. added this to the 7.29.0 milestone on Oct 29, 2026
  4. changed the title [-][java] UnusedPrivateMethod / UnusedPrivateField: false positives for fully-qualified @MethodSource / @FieldSource references[/-] [+][java] UnusedPrivateMethod / UnusedPrivateField: False positives for fully-qualified @MethodSource / @FieldSource references[/+] on Oct 29, 2026
  5. changed the title [-][java] UnusedPrivateMethod / UnusedPrivateField: False positives for fully-qualified @MethodSource / @FieldSource references[/-] [+][java] UnusedPrivateMethod / UnusedPrivateField: False positives for fully qualified @MethodSource / @FieldSource references[/+] on Oct 29, 2026
  6. added a commit that references this issue on Oct 29, 2026
    c44b87c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions