Why doesn't @DirtiesContext work in Spring Boot JUnit5(Jupiter) ? (OOM)

Viewed 592

I have a couple of integration in this fashion:

@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.
}


}

And

@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.
    }

}

Now how can I verify that @DirtiesContext is not working ?

It is because all my integration tests use Hikari to connect to the database:

On each new integration I see the number of the Hikari connection pool going up, like so:

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.

Each new test should be mutually exclusive of each other.

@DirtiesContext should have taken care of that, but it seems the previous beans are not being discarded.

Hence I am ending up with a memory leak.

 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

I am using :

Spring Boot 2.5.4
JUnit5 (Jupiter)

How can I solve this ?

Thanks

1 Answers

The Hikari poolName is incrementing through a JVM System property, this is why you can see it after each ApplicationContext destruction. This destruction effectively recreates ALL the beans of your Context.

Source from HikariConfig.class:

            synchronized(System.getProperties()) {
                String next = String.valueOf(Integer.getInteger("com.zaxxer.hikari.pool_number", 0) + 1);
                System.setProperty("com.zaxxer.hikari.pool_number", next);
                return "HikariPool-" + next;
            }

The memory problem is something else, you can try to increase the heap size of the JVM..

Related