小编ck1*_*ck1的帖子

对于 @TestConfiguration 类的 @SpringBootTest @Import 不执行任何操作,而 @ContextConfiguration 按预期覆盖

考虑以下集成测试注释:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
                properties = "spring.main.allow-bean-definition-overriding=true")
@ContextConfiguration(classes = {WorkerTestConfig.class})
//@Import(value = {WorkerTestConfig.class})
@ActiveProfiles({"dev","test"})
public class NumberServiceITest {
Run Code Online (Sandbox Code Playgroud)

WorkestTestConfig 的作用是在集成启动期间覆盖真实 bean/一组 bean,每当我使用@ContextConfiguration真实 bean 时,真实 bean 就会退出并使用 WorkerTestConfig 中的 bean,每当我使用真实 bean 时,@Import仍然会创建真实 bean 并导致测试失败。

WorkerTestConfig本身尽可能简单:

@TestConfiguration
public class WorkerTestConfig {

    @Primary
    @Bean
    public ScheduledExecutorService taskExecutor() {
        return DirectExecutorFactory.createSameThreadExecutor();
    }
}
Run Code Online (Sandbox Code Playgroud)

谁能解释一下 @SpringBootTest 注释的另一个神奇行为?如果您重现相同的行为,请确认,以便我可以转到问题跟踪器,因为我已经看到人们在此处 使用@Importwith ,并且在 Spring Boot 文档中没有任何内容禁止它:https: //docs.spring.io/spring-boot/文档/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-排除-config@SpringBootTest

完全不知道发生了什么。

版本:2.1.2.RELEASE

更新:

还尝试删除真正的bean,看看问题是否只是覆盖,但@Import注释只是死在水中,不起作用 - >甚至无法创建bean,@ContextConfiguration具有附加/覆盖行为,导入在全部。注释的完全限定导入是: import org.springframework.context.annotation.Import;

也试图从 到 …

spring-boot spring-boot-test

5
推荐指数
1
解决办法
8403
查看次数

标签 统计

spring-boot ×1

spring-boot-test ×1