java Spring Boot 测试类可以重用应用程序上下文以加快测试运行速度吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44180815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can Spring Boot test classes reuse application context for faster test run?
提问by vicusbass
@ContextConfiguration
location attribute does not make sense for Spring Boot integration testing. Is there any other way for reusing application context across multiple test classes annotated with @SpringBootTest
?
@ContextConfiguration
location 属性对于 Spring Boot 集成测试没有意义。有没有其他方法可以在多个用 注释的测试类中重用应用程序上下文@SpringBootTest
?
采纳答案by luboskrnac
Yes. Actually it is default behavior. The link point to Spring Framework docs, which is used by Spring Boot under the hood.
是的。实际上这是默认行为。该链接指向 Spring 框架文档,它由 Spring Boot 在后台使用。
BTW, context is reused by default also when @ContextConfiguration
is used as well.
顺便说一句,默认情况下,上下文也会在@ContextConfiguration
使用时重用。
回答by RoshanKumar Mutha
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
The above annotation says the complete context is loaded and same is used across the tests. It means it's loaded once only.
上面的注释说完整的上下文已加载,并且在测试中使用相同的上下文。这意味着它只加载一次。
Spring Boot provides a @SpringBootTest annotation which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests via SpringApplication
Spring Boot 提供了一个 @SpringBootTest 注释,当您需要 Spring Boot 功能时,它可以用作标准 spring-test @ContextConfiguration 注释的替代。注释的工作原理是通过 SpringApplication 创建在测试中使用的 ApplicationContext