Skip to content

When using JPA and ImportTestcontainers, test context may fail to refresh due to "Mapped port can only be obtained after the container is started" #40585

Closed
@ivangfr

Description

@ivangfr

Hi, I've upgraded this project from Spring Boot 3.1.4 to 3.2.5.

After that, I've started having issues in my test cases.

I am defining the MyContainers interface

public interface MyContainers {

    @Container
    @ServiceConnection
    PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:16.1");
}

Then, using the ImportTestcontainers annotation

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ImportTestcontainers(MyContainers.class)
class TeamDetailControllerTest implements MyContainers {
   ...
}

It seems that the test cases are not waiting for the containers to start

2024-04-30T15:20:41.230+02:00 ERROR 72102 --- [spring-data-jpa-relationships] [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource 
[org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0: Error creating bean with name 'dataSource' defined in class path resource 
[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate 
[com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Mapped port can only be obtained after the container is started

If I downgrade to Spring Boot 3.1.4, the test cases will run ok.

How to reproduce

  1. Clone: git clone https://github.com/ivangfr/spring-data-jpa-relationships.git
  2. Run: ./mvnw clean test

Thanks

Metadata

Metadata

Assignees

Labels

type: regressionA regression from a previous release

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions