Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Testcontainers test hang till timeout on "Waiting for database connection to become available at"?

When constructing a JUnit test using Testcontainers my test hangs until timeout after the message "Waiting for database connection to become available at" and the container logs, then helpfully shown, do not yield any error. I can even connect to the running docker container with my favourite JDBC-query-tool.

Currently I'm using the MySQL container.

like image 716
gkephorus Avatar asked Oct 19 '25 04:10

gkephorus


2 Answers

This is because I did not include the MySQL JDBC driver in my classpath. The Testcontainers does not log the fact that the 'SELECT 1', it suggests it's doing, is going wrong because of the missing driver. Normally it should fail with some timeout first until the container is up-and-running. But now it fails due to the missing driver and it somehow does not make a distinction.

like image 199
gkephorus Avatar answered Oct 21 '25 18:10

gkephorus


I had the same problem but with a different solution. You might have to check the version of the mysql docker image or versions of the Testcontainers dependencies. So this worked for me:

pom.xml:

    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>testcontainers</artifactId>
        <version>1.20.4</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>1.20.4</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>mysql</artifactId>
        <version>1.20.4</version>
        <scope>test</scope>
    </dependency>

TestClass

@Testcontainers
public class TestClass {
@Container
private static final MySQLContainer<?> mysqlContainer
        = new MySQLContainer<>("mysql:9.2.0")
        .withDatabaseName("testdatabase")
        .withUsername("test")
        .withPassword("test");
}
like image 29
Ben Kostka Avatar answered Oct 21 '25 16:10

Ben Kostka