From @sleepo581 in https://github.com/spring-projects/spring-boot/issues/38831#issuecomment-1867426247

After migrating to spring-boot v3.2.1 tests with @ImportTestcontainers are failing because container is not started. E.g.:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'spring.datasource.url' to java.lang.String:

    Reason: java.lang.IllegalStateException: Mapped port can only be obtained after the container is started`
public interface Containers {

    @Container
    PostgreSQLContainer<?> postgresContainer = new PostgreSQLContainer<>("postgres:16.1");

    @DynamicPropertySource
    static void setConnectionProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.datasource.url", postgresContainer::getJdbcUrl);
        registry.add("spring.datasource.password", postgresContainer::getPassword);
        registry.add("spring.datasource.username", postgresContainer::getUsername);
    }
 }

Comment From: sleepo581

@philwebb I've created a simple project with test setup that fails in v3.2.1, but works in 3.2.0.

https://github.com/sleepo581/testcontainers-bug/blob/master/spring-boot-3_2_0/src/test/java/com/example/springboot3_2_0/SpringBoot320ApplicationTests.java

It's project with 2 submodules (3.2.1/3.2.0) with identical code

Comment From: scottfrederick

Note that this only fails when @DynamicPropertySource is used. If @DynamicPropertySource is replaced by @ServiceConnection on the @Container bean field, then the tests will pass.

Comment From: goraxan

I confirm the same behaviour, any plans to fix it? Thanks

Comment From: scottfrederick

@goraxan The issue has a regression label and a milestone assigned, which indicates that we plan to address it in a future 3.2.x release.

Comment From: acemrek

Hello, With 3.2.5 DynamicPropertySource is not being called also, which works in 3.2.0 which blocks us from upgrading as of now.

  @Container
  PostgreSQLContainer<?> dbContainer = new PostgreSQLContainer<>(DockerImageName
      .parse("docker.io/postgres")
      .asCompatibleSubstituteFor("postgres")
      .withTag(DEFAULT_POSTGRESQL_TAG));

  @DynamicPropertySource
  static void postgresProperties(DynamicPropertyRegistry registry) {
    registry.add("spring.datasource.url", dbContainer::getJdbcUrl);
    registry.add("spring.datasource.username", dbContainer::getUsername);
    registry.add("spring.datasource.password", dbContainer::getPassword);
    registry.add("spring.datasource.driverClassName", dbContainer::getDriverClassName);
  }

and if I remove DynamicPropertySource and switch to serviceConnections, it does not see it all and spins up the in memory db.

Comment From: quaff

Hello, With 3.2.5 DynamicPropertySource is not being called also, which works in 3.2.0 which blocks us from upgrading as of now.

``` @Container PostgreSQLContainer<?> dbContainer = new PostgreSQLContainer<>(DockerImageName .parse("docker.io/postgres") .asCompatibleSubstituteFor("postgres") .withTag(DEFAULT_POSTGRESQL_TAG));

@DynamicPropertySource static void postgresProperties(DynamicPropertyRegistry registry) { registry.add("spring.datasource.url", dbContainer::getJdbcUrl); registry.add("spring.datasource.username", dbContainer::getUsername); registry.add("spring.datasource.password", dbContainer::getPassword); registry.add("spring.datasource.driverClassName", dbContainer::getDriverClassName); } ```

and if I remove DynamicPropertySource and switch to serviceConnections, it does not see it all and spins up the in memory db.

You should provide a minimized sample instead of code snippet.

Comment From: wilkinsona

@acemrek once you have a minimal sample to share, please open a new issue for your problem and we will investigate.

Comment From: ivangfr

I am facing the same issue. I've created this issue https://github.com/spring-projects/spring-boot/issues/40585, including a sample.