Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 添加“@SpringBootTest”注释会触发IntelliJ误报_Java_Spring_Spring Boot_Intellij Idea - Fatal编程技术网

Java 添加“@SpringBootTest”注释会触发IntelliJ误报

Java 添加“@SpringBootTest”注释会触发IntelliJ误报,java,spring,spring-boot,intellij-idea,Java,Spring,Spring Boot,Intellij Idea,所以如果你有一个测试类 class FooControllerIT{ @Autowired FooController controller; } IntelliJ声称,您在类中添加了一个@springbootest注释 无法自动连线。找不到“FooController”类型的bean 这是一个谎言,因为测试运行并顺利通过 如果我将@springbootest注释替换为@组件注释,则“错误”将消失(并且在我再次替换@springbootest注释时重新出现) 是什么导致了这种行为 (我

所以如果你有一个测试类

class FooControllerIT{
    @Autowired FooController controller;
}
IntelliJ声称,您在类中添加了一个
@springbootest
注释

无法自动连线。找不到“FooController”类型的bean

这是一个谎言,因为测试运行并顺利通过

如果我将
@springbootest
注释替换为
@组件
注释,则“错误”将消失(并且在我再次替换
@springbootest
注释时重新出现)

是什么导致了这种行为


(我使用的是ultimate 2019.1,使用的是Spring Boot 2.1.8-RELEASE)

找不到您的控制器的可能方式是,它与带注释的主应用程序类
@SpringBootApplication
不在同一(或更高)包中。如果不想移动控制器,可以创建一个新的配置类,该类将被注释为
@ComponentScan

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

即使您可以手动将此配置添加到测试spring上下文中:
@SpringBootTest(classes=FooConfig.class)

请添加测试类代码。你到底测试了什么?@ValeriyK。没关系。任何使用控制器的东西。它是否适用于Spring Boot较低版本,例如2.0.3?您是否在测试类的顶部添加了@RunWith(SpringRunner.class)?你使用testng还是junit?@ValeriyK。我们使用的是
@extendedwith(SpringExtension.class)
(对于Junit),但是是的,我添加了它。