简体   繁体   中英

Adding `@SpringBootTest` annotation triggers IntelliJ false positives

So If you have a test class

class FooControllerIT{
    @Autowired FooController controller;
}

and you add an @SpringBootTest annotation to the class, IntelliJ claims that

Could not autowire. No beans of 'FooController' type found.

Which is a lie because the tests run and pass just fine.

If I replace the @SpringBootTest annotation with, say, an @Component annotation, the "error" disappears (and re-appears when I substitute the @SpringBootTest annotation back in again).

What causes this behaviour?

(I'm on ultimate 2019.1, using Spring Boot 2.1.8-RELEASE)

The possible way that your Controller cannot be found is that it is not in the same (or higher) package as your main application class annotated @SpringBootApplication . If you don't want to move the Controller, you can create a new configuration class that will be annotated as @ComponentScan

@Configuration
@ComponentScan(basePackages = "com.your.controller.package")
public class FooConfig {
}

Even that you can add this config manually to your test spring context: @SpringBootTest(classes = FooConfig.class)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM