I have read that SpringBoot test runner will cache context between test classes. Having that in mind I have created single base class for integration tests like this
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ContextConfiguration(locations = "classpath:full-test-app-context.xml")
public class IntegrationTestBase {
....
}
and a simple "boot all" configuration in the xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
default-lazy-init="true"
>
<context:component-scan base-package="helpme">
</context:component-scan>
</beans>
However, I run all tests in the suite, context boots up multiple times. Indeed, consecutive boot takes less time, however I would expect that it would not happen at all.
For example, I can see banner beeing printed muliple time, multiple "application started" logs as well as multiple events of shuting down embedded database context. Why is this happening? How to actually cache whole context?
In some tests I am using @SpyBean
as @MockBean
would probably create new context. I have also copied single test class multiple times so the context of the test is exaclty the same - but still, context was not shared between those copied classes.
2020-10-03 12:35:14.237 INFO 10336 --- [ main] h.s.r.DeviceRegistrationServiceTest : Started DeviceRegistrationServiceTest in 8.436 seconds (JVM running for 10.315)
2020-10-03 12:35:17.073 INFO 10336 --- [ main] h.s.LocationServiceIntegrationTest : Started LocationServiceIntegrationTest in 2.579 seconds (JVM running for 13.15)
2020-10-03 12:35:19.277 INFO 10336 --- [ main] h.s.HelpPinServiceIntegrationTest : Started HelpPinServiceIntegrationTest in 1.67 seconds (JVM running for 15.355)
....
2020-10-03 12:35:24.738 INFO 10336 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'fTaskExecutor'
2020-10-03 12:35:24.738 INFO 10336 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2020-10-03 12:35:24.738 INFO 10336 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2020-10-03 12:35:24.741 INFO 10336 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-10-03 12:35:24.741 INFO 10336 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-10-03 12:35:24.741 INFO 10336 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-10-03 12:35:24.741 INFO 10336 --- [extShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
2020-10-03 12:35:24.741 INFO 10336 --- [extShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
2020-10-03 12:35:24.741 INFO 10336 --- [extShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
2020-10-03 12:35:24.750 INFO 10336 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2020-10-03 12:35:24.750 INFO 10336 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-3 - Shutdown initiated...
2020-10-03 12:35:24.750 INFO 10336 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-2 - Shutdown initiated...
2020-10-03 12:35:24.754 INFO 10336 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
2020-10-03 12:35:24.754 INFO 10336 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-2 - Shutdown completed.
2020-10-03 12:35:24.754 INFO 10336 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-3 - Shutdown completed.
Above you can see that app is starting multiple time (each time faster), and @ shutdown mutliple database connection pools are shutdown etc .