From 5e61b49f80291101c915146d07ae96bbafaac208 Mon Sep 17 00:00:00 2001 From: Mihails Volkovs Date: Mon, 13 Nov 2023 00:44:36 +0200 Subject: [PATCH] Waiting for DB listening port --- .../testcontainers/example/TestcontainersCloudFirstTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/cloud/testcontainers/example/TestcontainersCloudFirstTest.java b/src/test/java/cloud/testcontainers/example/TestcontainersCloudFirstTest.java index 204ac08..1cb8aaf 100644 --- a/src/test/java/cloud/testcontainers/example/TestcontainersCloudFirstTest.java +++ b/src/test/java/cloud/testcontainers/example/TestcontainersCloudFirstTest.java @@ -12,6 +12,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.testcontainers.DockerClientFactory; import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.images.builder.Transferable; import static org.assertj.core.api.Assertions.assertThat; @@ -22,7 +23,8 @@ public class TestcontainersCloudFirstTest { @Test public void createPostgreSQLContainer() throws SQLException { try (PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer<>("postgres:14-alpine") - .withCopyToContainer(Transferable.of(initsql), "/docker-entrypoint-initdb.d/init.sql")) { + .withCopyToContainer(Transferable.of(initsql), "/docker-entrypoint-initdb.d/init.sql") + .waitingFor(Wait.defaultWaitStrategy())) { postgreSQLContainer.start(); Connection connection = DriverManager.getConnection(postgreSQLContainer.getJdbcUrl(), postgreSQLContainer.getUsername(), postgreSQLContainer.getPassword()); PreparedStatement preparedStatement = connection.prepareStatement("SELECT COUNT(*) FROM guides");