Can Spring Boot test classes reuse application con

2019-06-15 16:17发布

问题:

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

回答1:

Yes. Actually it is default behavior. The link point to Spring Framework docs, which is used by Spring Boot under the hood.

BTW, context is reused by default also when @ContextConfiguration is used as well.



回答2:

@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