I have created a simple unit test but IntelliJ is incorrectly highlighting it red. marking it as an error
No beans?
As you can see below it passes the test? So it must be Autowired?
I have created a simple unit test but IntelliJ is incorrectly highlighting it red. marking it as an error
No beans?
As you can see below it passes the test? So it must be Autowired?
Sometimes you are required to indicate where @ComponentScan should scan for components. You can do so by passing the packages as parameter of this annotation, e.g:
However, as already mentioned, @SpringBootApplication annotation replaces @ComponentScan, hence in such cases you must do the same:
At least in my case, Intellij stopped complaining.
I just had to use @EnableAutoConfiguration to address it, however this error had no functional impact.
I had this same issue when creating a Spring Boot application using their
@SpringBootApplication
annotation. This annotation represents@Configuration
,@EnableAutoConfiguration
and@ComponentScan
according to the spring reference.As expected, the new annotation worked properly and my application ran smoothly but, Intellij kept complaining about unfulfilled
@Autowire
dependencies. As soon as I changed back to using@Configuration
,@EnableAutoConfiguration
and@ComponentScan
separately, the errors ceased. It seems Intellij 14.0.3 (and most likely, earlier versions too) is not yet configured to recognise the@SpringBootApplication
annotation.For now, if the errors disturb you that much, then revert back to those three separate annotations. Otherwise, ignore Intellij...your dependency resolution is correctly configured, since your test passes.
Always remember...
As long as your tests are passing you are good, hit
alt + enter
by taking the cursor over the error and inside the submenu of the first item you will findDisable Inspection
select thatThis seems to still be a bug in the latest IntelliJ and has to do with a possible caching issue?
If you add the @Repository annotation as mk321 mentioned above, save, then remove the annotation and save again, this fixes the problem.
My solution to this issue in my spring boot application was to open the spring application context and adding the class for the missing autowired bean manually!
(access via Project Structure menu or spring tool window... edit "Spring Application Context")
So instead of SpringApplicationContext just containing my ExampleApplication spring configuration it also contains the missing Bean:
SpringApplicationContext:
et voilà: The error message disappeared!