Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] UnusedPrivateField - false positive with Spring @SpyBean #4037

Closed
ghost opened this issue Jul 7, 2022 · 0 comments · Fixed by #4100
Closed

[java] UnusedPrivateField - false positive with Spring @SpyBean #4037

ghost opened this issue Jul 7, 2022 · 0 comments · Fixed by #4100
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@ghost
Copy link

ghost commented Jul 7, 2022

Affects PMD Version:6.47.0

Rule:UnusedPrivateField

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/latest/pmd_rules_java_bestpractices.html#unusedprivatefield

Description:
Hi, I found a false negative about the rule UnusedPrivateField, please read the following code example. In line 3, the code has an annotation @SpyBean, which means that the filed service could be used in testing. However, PMD reports a warning of this rule.

Besides, in src file, PMD has considered @MockBen, and it doesn't report identical warning for the field with MockBean. Hence, I think this is a false positive.

Code Sample demonstrating the issue:

import org.springframework.boot.test.mock.mockito.SpyBean;
public class C {
    @SpyBean
    private Service service;  // a false warning of this field
}

Expected outcome:

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

Running PMD through: [Maven]

@ghost ghost added the a:false-positive PMD flags a piece of code that is not problematic label Jul 7, 2022
@adangel adangel changed the title A false positive about the rule UnusedPrivateField [java] UnusedPrivateField - false positive with Mockito @SpyBean Jul 7, 2022
@adangel adangel changed the title [java] UnusedPrivateField - false positive with Mockito @SpyBean [java] UnusedPrivateField - false positive with Spring @SpyBean Jul 7, 2022
@adangel adangel added this to the 6.50.0 milestone Sep 30, 2022
adangel added a commit to adangel/pmd that referenced this issue Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant