I have created a simple unit test but IntelliJ is incorrectly highlighting it red. marking it as an error
No beans?
If you don't want to make any change to you code just to make your IDE happy. I have solved it by adding all components to the Spring facet.
Sometimes - in my case that is - the reason is a wrong import. I accidentally imported
import org.jvnet.hk2.annotations.Service
instead of
import org.springframework.stereotype.Service
by blindly accepting the first choice in Idea's suggested imports. Took me a few minutes the first time it happend :-)
All you need to do to make this work is the following code:
@ComponentScan
public class PriceWatchTest{
@Autowired
private PriceWatchJpaRepository priceWatchJpaRepository;
...
...
}
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...
Man is always greater than machine.
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:
@ComponentScan(basePackages={"path.to.my.components","path.to.my.othercomponents"})
However, as already mentioned, @SpringBootApplication annotation replaces @ComponentScan, hence in such cases you must do the same:
@SpringBootApplication(scanBasePackages={"path.to.my.components","path.to.my.othercomponents"})
At least in my case, Intellij stopped complaining.
What you need to do is add
@ComponentScan("package/include/your/annotation/component")
in AppConfiguration.java
.
Since I think your AppConfiguraion.java
's package is deeper than your annotation component (@Service, @Component...)'s package,
such as "package/include/your/annotation/component/deeper/config"
.