I am working on developing a few integration tests for my spring boot application. I am using testcontainers in order to create a MongoDB docker image. My code so far:
ContainerListener.java class:
public class ContainerListener implements TestListener {
@ClassRule public static Network sharedNetwork = Network.newNetwork(); @ClassRule public static GenericContainer mongoDBContainer = new GenericContainer("mongo:3.2.4").withNetwork(sharedNetwork) .withNetworkAliases("mongo").withExposedPorts(27017); public static MockServerContainer mockServerContainer = new MockServerContainer().withNetwork(sharedNetwork) .withNetworkAliases("mockserver").withExposedPorts(1080); public static int getMockPort() { return mockServerContainer.getMappedPort(1080); } public static int getMongoPort() { return mongoDBContainer.getMappedPort(27017); } public static void runAll() { List.of(mongoDBContainer, mockServerContainer).forEach(e -> e.start()); } public static void stopAll() { List.of(mongoDBContainer, mockServerContainer).forEach(e -> e.stop()); } }
And SomeControllerIntegrationTest.java class:
import io.restassured.RestAssured; import io.restassured.parsing.Parser; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; public class RatingControllerIntegrationTest { @BeforeAll public static void setuo() { ContainerListener.runAll(); } @AfterAll public static void tearDown() { ContainerListener.stopAll(); } @Test public void detailsTest() throws Exception { RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); RestAssured.defaultParser = Parser.JSON; given().log().all().when().get("http://localhost:8080/actuator/info/").then().log().all() .statusCode(200); } } Despite that after running the tests I get the following error:
org.testcontainers.containers.ContainerLaunchException: Container startup failed Caused by: org.testcontainers.containers.ContainerFetchException: Can't get Docker image: RemoteDockerImage(imageName=mongo:3.2.4, imagePullPolicy=DefaultPullPolicy()) at org.testcontainers.containers.GenericContainer.getDockerImageName(GenericContainer.java:1279) at org.testcontainers.containers.GenericContainer.logger(GenericContainer.java:613) at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:320) ... 58 more Caused by: java.lang.IllegalStateException: Can not connect to Ryuk at localhost:32770 at org.testcontainers.utility.ResourceReaper.start(ResourceReaper.java:176) at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:168) at org.testcontainers.LazyDockerClient.getDockerClient(LazyDockerClient.java:14) at org.testcontainers.LazyDockerClient.listImagesCmd(LazyDockerClient.java:12) at org.testcontainers.images.LocalImagesCache.maybeInitCache(LocalImagesCache.java:68) at org.testcontainers.images.LocalImagesCache.get(LocalImagesCache.java:32) at org.testcontainers.images.AbstractImagePullPolicy.shouldPull(AbstractImagePullPolicy.java:18) at org.testcontainers.images.RemoteDockerImage.resolve(RemoteDockerImage.java:59) at org.testcontainers.images.RemoteDockerImage.resolve(RemoteDockerImage.java:26) at org.testcontainers.utility.LazyFuture.getResolvedValue(LazyFuture.java:20) at org.testcontainers.utility.LazyFuture.get(LazyFuture.java:27) at org.testcontainers.containers.GenericContainer.getDockerImageName(GenericContainer.java:1277) ... 60 more