使用SpringBootTest webEnvironment MOCK和DEFINED_PORT进行测试时,意外的应用程序上下文缓存命中

问题描述

在同时使用@SpringBoottest(webEnvironment = SpringBoottest.WebEnvironment.MOCK)@SpringBoottest(webEnvironment = SpringBoottest.WebEnvironment.DEFINED_PORT)编写Spring测试时,我看到测试重复使用了相同的应用程序上下文。

@SpringBoottest(webEnvironment = SpringBoottest.WebEnvironment.DEFINED_PORT)
class DemoApplicationTestsDefinedPort {
    @Test
    void testDefinedPort() {}
}

@SpringBoottest(webEnvironment = SpringBoottest.WebEnvironment.MOCK)
class DemoApplicationTestsMock {
    @Test
    void mocktest() {}
}

使用logging.level.org.springframework.test.context.cache=DEBUG,相关的日志输出

...
Storing ApplicationContext [912966811] in cache under key [[WebMergedContextConfiguration@74287ea3 testClass = DemoApplicationTestsDefinedPort,...]]
...
Retrieved ApplicationContext [912966811] from cache with key [[WebMergedContextConfiguration@4dad8ec0 testClass = DemoApplicationTestsMock,...]]
...

testDefinedPort期望在server.port指定的端口上启动真实服务器。但是,根据执行顺序,testDefinedPort可能会重用mockTest创建的应用程序上下文。这是预期的行为吗?

更新:引发并标识为错误-https://github.com/spring-projects/spring-boot/issues/23085

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)