0

I have a SpringBoot application which I am trying to test with the help of Testcontainers. I have something like:

@SpringBootTest
public class DummyIT {

    @ClassRule
    public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer();

    static {
        postgreSQLContainer.start();
        String url = postgreSQLContainer.getJdbcUrl(); 
    }
}

Unfortunately Testcontainers use random ports and if I understood correctly we are not supposed to have it any other way, which means that the result of postgreSQLContainer.getJdbcUrl() is not deterministic.

Moreover, my application retrieves the database url from its application.properties and I was aiming at replacing such value from the one provided by postgreSQLContainer.getJdbcUrl(), before it is first used during runtime. Is it possible to achieve this?

Thank you for your help.

João Matos
  • 6,102
  • 5
  • 41
  • 76

1 Answers1

0

You can use Testcontainers JDBC URL support.

For example,

@SpringBootTest(
   properties = {
      "spring.datasource.url=jdbc:tc:postgresql:9.6.8://localhost/your-database",
      "spring.datasource.username=dummy-user",
      "spring.datasource.password=dummy-password",
      "spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect",
      "spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver"
})
public class DummyIT {
}

The Testcontainers driver ContainerDatabaseDriver will create and start a PostgreSQL server 9.6.8 with a database named your-database.

victor gallet
  • 1,819
  • 17
  • 25