简体   繁体   English

Spring Boot测试没有从同级包中找到bean

[英]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: 可以改为:

  • Add a nested class to your test, and annotate that class with @Configuration and the @ComponentScan annotation. 向您的测试中添加一个嵌套的类,并使用@Configuration@ComponentScan注释对该@ComponentScan注释。
  • Use an @Import annotation on the test class. 在测试类上使用@Import批注。 Note that @Import only allows to add individual classes to the context. 请注意, @Import仅允许将单个类添加到上下文中。
  • If don't mind adding the classes from the sibling package in other situations as well, you can also place the @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.

相关问题 使用@ComponentScan时,Spring Boot在兄弟包中找不到JpaRepositories或使用@EnableJpaRepositories时无法初始化它们 - Spring boot doesn't find JpaRepositories in sibling-package when using @ComponentScan or can't initialize them when using @EnableJpaRepositories Spring 引导找不到我的 Java bean 映射器 - Spring Boot doesn't find my Java bean mapper Spring Boot @ConditionalOnProperty 不加载 bean - Spring Boot @ConditionalOnProperty doesn't load bean Spring 启动:@Profile @Bean 组合不起作用 - Spring boot: @Profile @Bean combination doesn't work Spring Boot似乎没有找到Repository - Spring Boot doesn't seem to find Repository Spring引导无法从黄瓜测试上下文创建数据源bean - Spring boot cannot create datasource bean from cucumber test context 在 spring boot 中自动装配一个 bean,它在主 java 包中为空,但它在测试包中工作 - Autowiring a bean in spring boot and it is null in the main java package but it works in the test package 从测试/资源注入spring bean无法从main / java中找到类 - Injecting spring bean from test/resources can't find class from main/java 在Spring MVC中无法从单独的服务包中找到bean - Cannot find a bean from a separate service package in Spring MVC 为什么 Spring Boot 没有找到试图执行自动装配的 @Service bean? bean 存在但找不到它 - Why Spring Boot is not finding a @Service bean trying to perform autowiring? the bean exist but it can't find it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM