gpt4 book ai didi

spring-boot - 对于@SpringBootTest @TestConfiguration 类的@Import 什么都不做,而@ContextConfiguration 按预期覆盖

转载 作者:行者123 更新时间:2023-12-02 20:03:16 34 4
gpt4 key购买 nike

考虑以下集成测试注释:

@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 {

WorkestTestConfig 的作用是在集成启动期间覆盖真正的 bean/bean 集,无论何时我使用@Import 真正的 bean 仍然被创建并且没有通过测试。

WorkerTestConfig 本身尽可能简单:

@TestConfiguration
public class WorkerTestConfig {

@Primary
@Bean
public ScheduledExecutorService taskExecutor() {
return DirectExecutorFactory.createSameThreadExecutor();
}
}

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

完全不明白发生了什么。

版本:2.1.2.RELEASE

更新:

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

还试图从 @TestConfiguration 更改为 @Configuration 只是为了它,什么都没有。死了。

更新 2:

不过,@Import 可用于标准 Spring 测试:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {Some.class,
Some2WhichDependsOnWorkerTestConfig.class})

@Import(WorkerTestConfig.class)
@ActiveProfiles("test")
public class SomeOtherTest {

最佳答案

未定义 @Import 类在测试中使用时的处理顺序。添加用于测试的@Import 功能主要是为了允许其他 bean 轻松注册,无意将其用于替换 bean定义。

如果您想深入了解到底发生了什么,您可以打开 ConfigurationClassParser 并在 doProcessConfigurationClass 中添加一个条件断点。添加以下条件代码:

System.err.println(configClass);
return false;

现在,如果您调试应用程序,您将在处理配置类时获得附加输出。

当您使用没有@Importclasses 注释属性时,您将看到:

ConfigurationClass: beanName 'demoImportBugApplication', com.example.demoimportbug.DemoImportBugApplication
ConfigurationClass: beanName 'original', class path resource [com/example/demoimportbug/first/Original.class]
ConfigurationClass: beanName 'workerConfig', class path resource [com/example/demoimportbug/first/WorkerConfig.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/scheduling/annotation/ProxyAsyncConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/scheduling/annotation/ProxyAsyncConfiguration.class]
ConfigurationClass: beanName 'someTestSecondConfiguration', com.example.demoimportbug.second.SomeTestSecondConfiguration
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/context/PropertyPlaceholderAutoConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/cache/GenericCacheConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/cache/SimpleCacheConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/cache/NoOpCacheConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/context/ConfigurationPropertiesAutoConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfiguration.class]

当你使用没有 classes 属性的 @Import 时,你会得到:

ConfigurationClass: beanName 'org.springframework.boot.test.context.ImportsContextCustomizer$ImportsConfiguration', org.springframework.boot.test.context.ImportsContextCustomizer$ImportsConfiguration
ConfigurationClass: beanName 'null', class path resource [com/example/demoimportbug/first/SomeFirstUsingSecondConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [com/example/demoimportbug/second/SomeTestSecondConfiguration.class]
ConfigurationClass: beanName 'demoImportBugApplication', com.example.demoimportbug.DemoImportBugApplication
ConfigurationClass: beanName 'original', class path resource [com/example/demoimportbug/first/Original.class]
ConfigurationClass: beanName 'workerConfig', class path resource [com/example/demoimportbug/first/WorkerConfig.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/scheduling/annotation/ProxyAsyncConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/scheduling/annotation/ProxyAsyncConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/context/PropertyPlaceholderAutoConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/cache/GenericCacheConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/cache/SimpleCacheConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/cache/NoOpCacheConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/context/ConfigurationPropertiesAutoConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/info/ProjectInfoAutoConfiguration.class]
ConfigurationClass: beanName 'null', class path resource [org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfiguration.class]

第一个版本在 SomeTestSecondConfiguration 之前加载 WorkerConfig 而第二个版本在 WorkerConfig 之前加载 SomeTestSecondConfiguration

您还会注意到第二个版本有一个 ImportsContextCustomizer$ImportsConfiguration 类,它会触发额外的导入。

如果您查看 SpringBootTestContextBootstrapper,您可以在 getOrFindConfigurationClasses 方法中看到定义了顺序,并且您的附加测试类将始终列在主要配置之后。

tl;dr 如果您需要定义顺序,请使用 classes 属性。如果您想添加额外的 beans 并且您不想覆盖任何东西,请使用 @Import

您可能还想看看 @MockBean,它提供了一种用模拟替换 bean 的更健壮的方法。

关于spring-boot - 对于@SpringBootTest @TestConfiguration 类的@Import 什么都不做,而@ContextConfiguration 按预期覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55326492/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com