GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] ImmutableField reports fields annotated with @Autowired (Spring) and @Mock (Mockito) · Issue #3874 · pmd/pmd

[java] ImmutableField reports fields annotated with @Autowired (Spring) and @Mock (Mockito) #3874

New issue
Copy link
New issue
Copy link
Closed
#3964
Closed
[java] ImmutableField reports fields annotated with @Autowired (Spring) and @Mock (Mockito)#3874
#3964
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematicgood first issueA great starting point for new contributors
Milestone
6.46.0

Description

@C-Otto
C-Otto
opened on Mar 28, 2022
Issue body actions

Also see my comment in #3855.

Affects PMD Version:
6.44.0

Rule:
ImmutableField
https://pmd.github.io/pmd-6.44.0/pmd_rules_java_design.html#immutablefield

Description:
Fields annotated with Spring's @Autowired are reported as "could be final", although the field needs to be non-final in order for Spring to set it via reflection. While I prefer constructor injection for production code (where this false positive is not triggered), using annotated fields is very common for (integration) tests as shown below. This issue also exist for @Mock (Mockito) and several another annotations.

Note that this seems to be some kind of regression, as I didn't experience this with 6.43.0. I am aware that I can exclude annotations as shown in the documentation, but given the fact that it worked before, I'd like to voice my concerns and let you decide if this is a bug or not.

Code Sample demonstrating the issue:

@DataJpaTest
class SomeRepositoryIT {
    @Autowired
    private SomeRepository repository;

    ...
}

Expected outcome:
PMD reports a violation for field repository, but that's wrong. That's a false positive.

Running PMD through:
Gradle

Reacted by Cledyn and Łukasz Sikora

Activity

  1. C-Otto
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Mar 28, 2022
  2. Thunderforge commented on Apr 27, 2022

    @Thunderforge
    Thunderforge
    on Apr 27, 2022
    Contributor
    More actions

    This also affects mocked objects with Mockito annotations (which at least in our projects are on fields that in the main class are annotated with @Autowired)

    @Service
    public class FooService {
    
        @Autowired
        private BarService barService
    
        public void doFoo() {
            barService.doBar();
        }
    }
    @ExtendWith(MockitoExtension.class)
    class FooServiceTest {
        @InjectMocks
        private FooService fooService;
    
        @Mock
        private BarService barService;
    
        @Test
        void fooServiceCallsBarService() {
            fooService.doFoo();
    
            verify(barService)
                .doBar();
        }
    }

    PMD complains that the Mockito-annotated objects should be final, which isn't possible.

  3. adangel
    added
    good first issueA great starting point for new contributors
    on Apr 29, 2022
  4. adangel
    changed the title [-][java] ImmutableField reports fields annotated with @Autowired (Spring)[/-] [+][java] ImmutableField reports fields annotated with @Autowired (Spring) and @Mock (Mockito)[/+] on May 20, 2022
  5. adangel
    added this to the 6.46.0 milestone on May 20, 2022
  6. adangel
    added 2 commits that reference this issue on May 20, 2022

    [doc] Update release notes (pmd#3874, pmd#3964)

    2daa338

    Merge pull request pmd#3964 from lukelukes:master

    5736b38
  7. adangel
    closed this as completedin 5736b38on May 20, 2022
  8. adangel
    closed this as completedin #3964on May 20, 2022
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-positivePMD flags a piece of code that is not problematicgood first issueA great starting point for new contributors

    Type

    No type

    Projects

    No projects

      Milestone

      • 6.46.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.