Issue with Testcontainers @ServiceConnection in AbstractIntegrationTest When Running Multiple Test Classes

I’m experiencing an issue with my integration tests using Testcontainers and the new @ServiceConnection annotation. I have an abstract class AbstractIntegrationTest which is extended by multiple test classes. The relevant code is as follows:

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@Testcontainers
@ActiveProfiles("test")
public abstract class AbstractIntegrationTest {

    @Autowired protected MockMvc client;
    @Autowired ObjectMapper mapper;

    @Container
    @ServiceConnection
    static PostgreSQLContainer<?> postgresDB = new PostgreSQLContainer<>("postgres:16.3");
}

When I run each test class individually, they pass without any issues. However, when I run all test classes together, only the first test class executes successfully while the subsequent ones fail with the following error logs:

2024-05-21T22:53:27.678+02:00  INFO 369738 --- [    Test worker] tc.postgres:16.3                         : Creating container for image: postgres:16.3
2024-05-21T22:53:27.703+02:00  INFO 369738 --- [    Test worker] tc.postgres:16.3                         : Container postgres:16.3 is starting: 2214d73f09c3600beeadbd6a9031473e2dae9526c987991371a9f4fd22af5c59
2024-05-21T22:53:28.932+02:00  INFO 369738 --- [    Test worker] tc.postgres:16.3                         : Container postgres:16.3 started in PT1.253347718S
2024-05-21T22:53:28.932+02:00  INFO 369738 --- [    Test worker] tc.postgres:16.3                         : Container is started (JDBC URL: jdbc:postgresql://localhost:32779/test?loggerLevel=OFF)
2024-05-21T22:53:29.044+02:00  WARN 369738 --- [    Test worker] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@5c2ae7d7 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
2024-05-21T22:53:29.045+02:00  WARN 369738 --- [    Test worker] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@7e122a87 (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
.
.
.
.
2024-05-21T22:53:59.046+02:00 ERROR 369738 --- [    Test worker] o.h.engine.jdbc.spi.SqlExceptionHelper   : Connection to localhost:32778 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

The port in the error message is one less than the port where the container is started. It seems that the @ServiceConnection is not refreshing with the new Docker initialization. I suspect the issue is related to the service connection not being updated properly or the Docker container reinitializing. Ideally, I would like to prevent the Docker container from reinitializing or ensure that the service connection refreshes correctly, but I haven’t found a way to achieve either.

I have tried using the withReuse(true) method with no success:

@Container
@ServiceConnection
static PostgreSQLContainer<?> postgresDB = new PostgreSQLContainer<>("postgres:16.3").withReuse(true);

If I avoid using the @ServiceConnection annotation, there are workarounds that may help, but I prefer to utilize this new functionality. How can I resolve this issue while keeping @ServiceConnection in place?

Any insights or solutions would be greatly appreciated!

1

I managed to solve the issue by moving the Testcontainers configuration into a separate configuration class and removing the static container. This prevents the container from being reinstantiated for each test class. Here’s what I did:

First, I created a configuration class:

@TestConfiguration
public class TestContainersConfiguration {

    @Bean
    @ServiceConnection
    public PostgreSQLContainer<?> postgresDB() {
        return new PostgreSQLContainer<>("postgres:16.3");
    }
}

Then, I updated the AbstractIntegrationTest class to import this configuration:

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@Testcontainers
@ActiveProfiles("test")
@Import(TestContainersConfiguration.class)
public abstract class AbstractIntegrationTest {

    @Autowired protected MockMvc client;
    @Autowired ObjectMapper mapper;

    // Removed the static container
}

I found the answer here: Testcontainers start first test so long.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật