@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
?
We can have multiple application contexts that share a parent-child relationship. A context hierarchy allows multiple child contexts to share beans which reside in the parent context. Each child context can override configuration inherited from the parent context.
Spring Boot injects the application context into the parameter of the setApplicationContext() method, where we get the Id of the Spring application. (The Id here is the name of the application.) In the Application , we create a bean, call its method and set up the Spring Boot application.
With the @SpringBootTest annotation, Spring Boot provides a convenient way to start up an application context to be used in a test.
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 through SpringApplication .
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.
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With