【发布时间】:2022-02-01 19:56:20
【问题描述】:
我有几个以这种方式集成:
@SpringBootTest
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@ActiveProfiles({"test", "test-batch"})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class HierarchyIntegrationTest {
@Test
@DirtiesContext
void hierarchy_test_a() {
//test code here.
}
@Test
@DirtiesContext
void hierarchy_test_b() {
//test code here.
}
@Test
@DirtiesContext
void hierarchy_test_c() {
//test code here.
}
}
和
@SpringBootTest
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@ActiveProfiles("test")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class TicketPriceIntegrationTest {
@Test
@DirtiesContext
void ticket_test_a() {
//test code here.
}
@Test
@DirtiesContext
void ticket_test_b() {
//test code here.
}
@Test
@DirtiesContext
void ticket_test_c() {
//test code here.
}
}
现在我如何验证@DirtiesContext 不起作用?
因为我所有的集成测试都使用Hikari连接数据库:
在每次新的集成中,我都会看到 Hikari 连接池的数量在增加,如下所示:
00:47:02.450 [INFO ] c.z.h.HikariDataSource - HikariPool-20 - Shutdown initiated...
00:47:03.061 [INFO ] c.z.h.HikariDataSource - HikariPool-20 - Shutdown completed.
00:47:02.450 [INFO ] c.z.h.HikariDataSource - HikariPool-21 - Shutdown initiated...
00:47:03.061 [INFO ] c.z.h.HikariDataSource - HikariPool-21 - Shutdown completed.
00:47:02.450 [INFO ] c.z.h.HikariDataSource - HikariPool-22 - Shutdown initiated...
00:47:03.061 [INFO ] c.z.h.HikariDataSource - HikariPool-22 - Shutdown completed.
每个新测试都应该相互排斥。
@DirtiesContext 应该已经处理好了,但是之前的 bean 似乎没有被丢弃。
因此我最终会出现内存泄漏。
00:50:36.597 [ERROR] o.s.t.c.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@550625bf] to prepare test instance [com.item.integration.IntegrationTest@5b59e4c6]
java.lang.OutOfMemoryError: Java heap space
我正在使用:
Spring Boot 2.5.4
JUnit5 (Jupiter)
我该如何解决这个问题?
谢谢
【问题讨论】:
标签: spring-boot junit spring-boot-test applicationcontext junit-jupiter