From 779c4d296da1d38d8485563dc69ccf8a0efaae71 Mon Sep 17 00:00:00 2001 From: lobo Date: Thu, 24 Sep 2026 14:18:32 +0200 Subject: [PATCH] test: share one Postgres container across test classes The per-class container was stopped under the Spring context that the next integration test class reused, so health answered 503 there. --- .../shared/test/AbstractIntegrationTest.java | 10 ++++++---- .../shared/test/AbstractPersistenceTest.java | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/test/java/auth_service/shared/test/AbstractIntegrationTest.java b/src/test/java/auth_service/shared/test/AbstractIntegrationTest.java index dbf8ec2..4740a25 100644 --- a/src/test/java/auth_service/shared/test/AbstractIntegrationTest.java +++ b/src/test/java/auth_service/shared/test/AbstractIntegrationTest.java @@ -5,8 +5,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.boot.testcontainers.service.connection.ServiceConnection; import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; /** * Base class for integration tests. @@ -15,11 +13,15 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("IntegrationTest") @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ActiveProfiles("dev") -@Testcontainers public abstract class AbstractIntegrationTest { - @Container + // One container per JVM, not per class: Spring caches the context across test classes, + // and a per-class container would be stopped under the cached context. @ServiceConnection static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:17-alpine"); + + static { + postgres.start(); + } } diff --git a/src/test/java/auth_service/shared/test/AbstractPersistenceTest.java b/src/test/java/auth_service/shared/test/AbstractPersistenceTest.java index eb0d395..9ddd1aa 100644 --- a/src/test/java/auth_service/shared/test/AbstractPersistenceTest.java +++ b/src/test/java/auth_service/shared/test/AbstractPersistenceTest.java @@ -5,8 +5,6 @@ import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest; import org.springframework.boot.jdbc.test.autoconfigure.AutoConfigureTestDatabase; import org.springframework.boot.testcontainers.service.connection.ServiceConnection; import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; /** * Base class for persistence tests. @@ -15,10 +13,14 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("PersistenceTest") @DataJpaTest @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) -@Testcontainers public abstract class AbstractPersistenceTest { - @Container + // One container per JVM, not per class: Spring caches the context across test classes, + // and a per-class container would be stopped under the cached context. @ServiceConnection static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:17-alpine"); + + static { + postgres.start(); + } }