Affects PMD Version:
6.53.0+
7.0.0
Rule:
UnusedPrivateMethod
https://pmd.github.io/latest/pmd_rules_java_bestpractices.html#unusedprivatemethod
Description:
When using junit 5 parameterized test method source annotation, @MethodSource, the default behavior of this annotation is as described by the documentation below:
If no factory method names are declared, a method within the test class that has the same name as the test method will be used as the factory method by default.
https://junit.org/junit5/docs/5.9.0/api/org.junit.jupiter.params/org/junit/jupiter/params/provider/MethodSource.html
When this default behavior is used pmd reports the factory method as an UnusedPrivateMethod. This is a false positive.
When a value is provided to the annotation @MethodSource there is no false positive, addressed in the below issues:
Code Sample demonstrating the issue:
//false positive triggered private static Stream<Arguments> testGetUsername_noMethodSourceValue() { return Stream.of( Arguments.of("foo"), Arguments.of("bar"), Arguments.of("baz") ); } @MethodSource @ParameterizedTest void testGetUsername_noMethodSourceValue(String username) { User sut = new User(username); Assertions.assertEquals(username, sut.getUsername()); } //false positive not triggered private static Stream<Arguments> testGetUsername_methodSourceValue() { return Stream.of( Arguments.of("foo"), Arguments.of("bar"), Arguments.of("baz") ); } @MethodSource("testGetUsername_methodSourceValue") @ParameterizedTest void testGetUsername_methodSourceValue(String username) { sut = new User(username); Assertions.assertEquals(username, sut.getUsername()); }
Runnable sample project https://github.com/mluckam/pmd_examples/tree/UnusedPrivateMethod
Expected outcome:
PMD reports a violation at line 2. This is a false positive since the parameterized test of the same name, testGetUsername_noMethodSourceValue, uses the private method as a source of input.
Running PMD through: [Gradle]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4