如何解决使用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 举报,一经查实,本站将立刻删除。