Skip to content

False positive "Unnecessary @Autowired" warning when using abstract classes #985

Closed

Description

@pietro-saccani

Hello, I'm on Eclipse; I'm using an abstract class with a certain constructor, and if in a class extending that one I put @Autowired on the constructor (that needs to call super(<arguments>) inside), STS flags that annotation as unnecessary.
If I remove it, though, an exception is thrown: org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter <type and name of the parameter> in constructor <description of the constructor>.
So, that annotation is for sure not unnecessary.

I don't know which class is responsible for this warning, but while I was looking for it I've noticed that the AutowiredConstructorReconciler class has a typo in it (says "Unnecesary", with one "s").

Here's a repo with a super simple project that reproduces the issue on STS 4.17.2.
The project contains two test classes: the abstract one whose constructor wants a RestTemplateBuilder argument (I simply used the first @bean type that came to my mind, could be anything else); the other class extends it and has @Autowired on the constructor.
If you try to execute the unit test, it will work. But if you remove that @Autowired as the warning says, you'll get the exception I mentioned above.

Activity

  1. added
    for: eclipse
    for: vscode
    and removed on Mar 2, 2023
  2. added this to the 4.18.0.RELEASE milestone on Mar 2, 2023
  3. BoykoAlex commented on Mar 3, 2023

    @BoykoAlex
    Contributor

    @pietro-saccani thanks very much for the detailed issue with example project!

    AutowiredConstructorReconciler is a dead unused class. I have removed ii - thanks for bringing this up! This dead code was left in the repo for some time.

    As for the false-positive. We exclude classes annotated with @SpringBootTest but didn't account that this annotation is inherited therefore i have enhanced the exclusion check to also test the super types for @SpringBootTest

    Should be fixed with 9cf2d7e

    You could try STS4 eclipse snapshot update site tomorrow (see http://dist.springsource.com/snapshot/STS4/nightly-distributions.html)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

for: eclipsefor: vscodetheme: validationtype: bug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions