springtestcontainers

Use custom testcontainer in CI build


I have junit tests in spring boot test which use PostgreSQLContainer testcontainers for integration testing. These work fine in my AzureDevops CI pipeline.

Now I've introduced a custom service using a custom image

new GenericContainer(DockerImageName.parse("myregistry.azurecr.io/myimage:latest"))
      .withExposedPorts(1234);

Then I try to connect the service in the container

String url = "http://localhost:" + server.getMappedPort(1234);

The tests are running locally but fail in my AzureDevops CI pipeline. In the CI pipeline the tests cannot connect to the service in the container. I get a Connection refused.


Solution

  • You should consider these points:

    Use a WaitStrategy to wait not only for the container startup but also for the service to be ready. E.g. you can look for a startup message in the logs:

    .waitingFor((new LogMessageWaitStrategy()).withRegEx(".*Server Started.*\\s").withStartupTimeout(Duration.of(60L, ChronoUnit.SECONDS)))
    

    A container started in AzureDevops doesn't use localhost. Therefore you should use server.getHost() to determine the host:

    String baseUrl = "http://"+server.getHost() + ":" + server.getMappedPort(1234);