如何在Spring Boot中的集成测试案例之间完全缓存应用程序上下文?

问题描述

我已经读过SpringBoot测试运行器将在测试类之间缓存上下文。考虑到这一点,我为这种集成测试创建了单个基类

@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ContextConfiguration(locations = "classpath:full-test-app-context.xml")
public class IntegrationTestBase {
....
}

和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>

但是,我运行套件中的所有测试,上下文会多次启动。确实,连续启动花费的时间更少,但是我希望它根本不会发生。

例如,我可以看到横幅蜂鸣的打印时间,多个“应用程序启动”日志以及多个关闭嵌入式数据库上下文的事件。为什么会这样呢?如何实际缓存整个上下文?

在某些测试中,我将@SpyBean用作@MockBean可能会创建新的上下文。我还多次复制了单个测试类,因此测试的上下文完全相同-但仍然没有在这些复制的类之间共享上下文。

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.

上面您可以看到应用程序正在启动多个时间(每个时间都更快),并且@ shutdown mutliple数据库连接池处于shutdown等状态。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...