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

2023-12-24

考虑以下集成测试注释:

@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 集,每当我使用@ContextConfiguration每当我使用时,真正的 bean 就会退出,并使用 WorkerTestConfig 中的 bean@Import真正的 bean 仍然被创建并且未通过测试。

The WorkerTestConfig其本身尽可能简单:

@TestConfiguration
public class WorkerTestConfig {

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

谁能解释一下 @SpringBootTest 注释的另一个神奇行为?如果您重现相同的行为,请确认,以便我可以转到问题跟踪器,因为我看到人们使用@Import with @SpringBootTest在这里,Spring Boot 文档中没有任何内容禁止它:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-execution-config 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

Update:

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

也尝试过改变@TestConfiguration to @Configuration只是为了它,什么都没有。死的。

更新2:

The @Import但适用于标准弹簧测试:

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

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

的顺序是@Import类在未定义的测试中使用时进行处理。这@Import添加测试功能主要是为了允许额外的beans 很容易注册,无意将其用于replace豆定义。

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

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

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

当你使用classes没有注释属性@Import你会看到的:

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]

当您使用@Import没有classes您将获得的属性:

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]

第一个版本加载WorkerConfig before SomeTestSecondConfiguration而第二个版本加载SomeTestSecondConfiguration before WorkerConfig.

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

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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • 内容安全策略:允许所有外部图像?

    我希望只允许来自本地服务器的脚本 但有某些例外 例如 jQuery 等 但可以灵活地加载外部图像 我知道有一个像这样的指令 Content Security Policy script src self https apis google
  • 将 ADT 和 Android sdk 工具更新到最新版本 22 后出现类未找到错误

    早些时候它工作正常 当我将我的 adt 和 android sdk 工具更新到最新版本 rev 22 时 我的应用程序停止工作 下面是 logcat 输出 请帮忙 05 16 16 36 01 922 E AndroidRuntime 12
  • 使用 Highcharts.js 的圆边仪表

    我正在使用 Highcharts 创建自定义仪表 窗格形状应如所附图片所示为圆形 想知道是否有人知道如何使用该库实现此布局 这是http jsfiddle net ao9fv2yh http jsfiddle net ao9fv2yh 我正
  • Git:忽略已编译的 Google Go

    我编译的 Go 代码在 Linux 上没有以扩展结尾 对于处理忽略 gitignore 文件中的这些内容有什么技巧吗 如果您正在使用go您可以使用构建代码的工具 o标志来指定输出文件名 因此您可以使用go build o bin elf然后
  • .NET 世界中的 ORM 和 SOA [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 根据我的经验 NET 的主要 ORM 框架 NHibernate http nhforge org Linq 转 Sql http msdn m
  • 为什么 Android 会截断我的 ActionBar 标题?

    在我的应用程序中 我更改了 ActionBar 中显示的每个片段的标题 当我第一次启动我的应用程序时 我收到了一个请求列表 因此我的标题是 我的请求 20 然后 当您单击该列表中的某个项目时 它会替换我的活动中的一个片段 并将标题设置为 操
  • 如何在子例程内引用哈希?

    我正在尝试使用模块的 tie 功能配置 Ini文件 http search cpan org shlomif Config IniFiles 2 65 lib Config IniFiles pm但我不知道如何引用子例程内部的哈希 如果我从
  • 查询或过滤最小字段值?

    示例 存储在索引中的文档表示测试分数和有关每个测试的元数据 test 1 user 1 score 100 meta other data test 2 user 2 score 65 meta other data test 3 user
  • Pylint 未在 VScode 中按预期运行

    当我通过 shell 运行时pylint pylint decorator py No config file found using default configuration Module decorator C 7 0 Unneces
  • EC2 无法解析私有 DNS 主机名

    我是 Amazon Web Service AWS 的新手 我刚刚创建了一个 VPC 和一个位于其中的子网 但是 我无法解析该子网中的任何主机名 当我停留在该子网中的任何主机中时 ec2 user ip 192 168 1 86 nsloo
  • 根据 rowwise 函数 (dplyr) 过滤行

    您能帮我在下面的最后一个命令中使用过滤dplyr代替apply 我试图解决发布的问题here https twitter com delta dc status 585478403463245826 photo 1 library gtoo
  • 使用Spring Beans和不使用Spring Beans有什么区别?

    可能我会得到很多反对票 但是对于我来说是否使用 bean 的所有事实都让我感到困惑 让我们假设这个例子 interface ICurrency String getSymbol public class CurrencyProcessor
  • boost::filesystem::path(std::wstring) 抛出异常

    这段代码 boost filesystem is directory usr include 工作正常 这段代码 boost filesystem is directory L usr include 抛出异常 抛出一个后终止调用 std
  • 首次加载时 WPF 验证不会触发

    在 Prism 应用程序中 我想使用验证 我已经在 ViewModel 中实现了 INotifyDataError 接口 但我发现首次加载控件时不会触发验证解决方案 然后我发现了同样的问题 比如 wpf 验证绑定在首次加载时未触发 我找到了
  • 使用 ACR122 在卡模拟模式下将数据写入 Nexus 4

    我正在尝试通过 NFC 即卡模拟模式 向 Nexus 4 发送一些数据 我尝试了许多命令 APDU 例如写入和更新 APDU 但无法让它们工作 我想说的是 我想在选择 APDU 命令后向手机发送一些数据 不是 AID 提前致谢 Bader
  • Doctrine 迁移:“”命名空间中没有定义命令

    我正在尝试将 Doctrine Migrations 设置为独立程序 但遇到了一些麻烦 我在同一个文件夹中有doctrine migrations phar 和migrations yml migrations yml 包含以下内容 nam
  • 邮件:无法打开流:权限被拒绝?

    我在使用 php 发送邮件时收到此警告 警告 邮件 1 function mail 无法打开流 home 中的权限被拒绝 使用 ssmtp 和 gmail 作为 smtp PHP 5 3 1 日志中没有任何内容 没有错误 邮件到达目的地 文
  • Vue npm runserve 在随机端口上启动

    我正在尝试在端口 8080 上运行 Vue 但无法使其工作 我刚刚创建了一个全新的项目vue create 并运行它npm run serve 这会在随机端口上启动应用程序 第一次尝试 运行 npm runserve 无需任何额外配置 np
  • 确定应用程序是否正常关闭

    有没有办法判断jvm是否正常关闭 shutdown hook只能产生一个线程 有没有办法判断当时JVM是正常存在还是异常存在 您可以在启动时写入一个文件 并在正常退出时再次删除它 如果 JVM 消失了 但文件仍然存在 您就知道它崩溃了或者以
  • 对于 @TestConfiguration 类的 @SpringBootTest @Import 不执行任何操作,而 @ContextConfiguration 按预期覆盖

    考虑以下集成测试注释 RunWith SpringRunner class SpringBootTest webEnvironment SpringBootTest WebEnvironment NONE properties spring