[英]Spring Boot test doesn't find bean from sibling package
I have a @SpringBootTest
annotated test class which wants to make use of a test utility: 我有一个@SpringBootTest
注释的测试类,它想利用一个测试工具:
package org.myproject.server;
// ...
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ServerITest {
private @Autowired TestHelperBean helper;
// ...
}
This works fine if the TestHelperBean
is defined in the same package as the test class (or in a sub-package). 如果TestHelperBean
是在与测试类相同的程序包(或子程序包)中定义的,则此方法很好。
package org.myproject.server;
import org.springframework.stereotype.Component;
@Component
public class TestHelperBean {
// ...
}
If I move it to a sibling package though, the component is not found: 如果我将其移动到同级包中,则找不到该组件:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.myproject. org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'org.myproject的合格bean。 testutils .TestHelperBean' available: expected at least 1 bean which qualifies as autowire candidate. 可以使用testutils .TestHelperBean:至少应有1个符合自动装配候选条件的bean。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
I guess that component scanning by default only looks in the test class' package and sub-packages – but is there a way to override this default? 我猜默认情况下组件扫描仅在测试类的程序包和子程序包中查找-但是有没有办法覆盖此默认值? I tried to add the @ComponentScan
annotation to my test, but this didn't seem to have any effect: 我试图将@ComponentScan
批注添加到测试中,但这似乎没有任何效果:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ComponentScan("org.myproject")
public class ServerITest {
// ...
}
Is there a way to use beans from sibling packages in Spring Boot tests? 在Spring Boot测试中,有没有办法使用同级包中的bean?
The @ComponentScan
annotation doesn't work if placed on the test class . 如果将@ComponentScan
批注放置在测试类上,则该批注将不起作用 。
This can be done instead: 可以改为:
@Configuration
and the @ComponentScan
annotation. 向您的测试中添加一个嵌套的类,并使用@Configuration
和@ComponentScan
注释对该@ComponentScan
注释。 @Import
annotation on the test class. 在测试类上使用@Import
批注。 Note that @Import
only allows to add individual classes to the context. 请注意, @Import
仅允许将单个类添加到上下文中。 @ComponentScan
annotation on any @Configuration
class (or @Component
, @Service
, etc.) that is already included in the context. 如果在其他情况下也不介意从同级包中添加类,则还可以将@ComponentScan
批注放置在上下文中已包含的任何@Configuration
类(或@Component
, @Service
@Component
等)上。 在组件扫描中,您可以添加多个需要扫描的软件包
@ComponentScan({"org.myproject","org.myproject. server","org.myproject. sibilings"})
With componentscan annotation you can specify '*' to cover all subpackages under base package as well. 使用componentscan批注,您还可以指定“ *”以覆盖基本包下的所有子包。
@ComponentScan({"org.myproject.*", "org.newproj.*"})
It covers all subpackages under "org.myproject" and "org.newproj". 它涵盖了“ org.myproject”和“ org.newproj”下的所有子包。
Example package structure 封装结构示例
org.myproject
org.myproject.abc
org.myproject.abcd
org.myproject.xyz.abc
org.newproj
org.newproj.abc
org.newproj.xyz
OR 要么
Register bean with Configuration/SpringBootApplication class 用Configuration / SpringBootApplication类注册bean
@Bean
private TestHelperBean helper() {
return new TestHelperBean();
}
The other answers regarding the use of ComponentScan
are correct. 有关使用ComponentScan
的其他答案是正确的。 However, the Spring Boot documentation strongly advises that "your main application class be in a root package above other classes". 但是, Spring Boot文档强烈建议“您的主应用程序类在其他类之上的根包中”。 From personal experience I can say that deviating from this practice will result in more trouble than it's worth. 根据个人经验,我可以说偏离这种做法会导致更多的麻烦,而不是值得的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.