Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Spring Boot test classes reuse application context for faster test run?

Tags:

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

like image 647
vicusbass Avatar asked May 25 '17 12:05

vicusbass


People also ask

Can spring have multiple application contexts?

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.

Does spring boot use application 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.

Does SpringBootTest start application?

With the @SpringBootTest annotation, Spring Boot provides a convenient way to start up an application context to be used in a test.

Which of the following annotation can be used for testing spring boot application which creates the entire context without starting the server?

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 .


2 Answers

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.

like image 154
luboskrnac Avatar answered Sep 19 '22 15:09

luboskrnac


@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

like image 44
RoshanKumar Mutha Avatar answered Sep 19 '22 15:09

RoshanKumar Mutha