使用测试容器和泊坞窗的集成测试问题“由:java.lang.NullPointerException引起:未指定containerId”

时间:2019-02-04 23:02:40

标签: mongodb spring-boot docker testcontainers

我对 testcontainers 非常陌生。我什至不确定我要做什么,或者是否有更好的方法。

目标:为我的MongoDb存储库编写一个集成测试。我正在使用Docker,Maven。 Jdk11,Spring Boot 2。

我的父母考试班

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {PostServiceApplication.class},
        properties = "spring.cloud.config.enabled:true",
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class AbstractIntegrationTest {

    private static final String MONGO_CONTAINER = "mongo:3.1.5";
    private static final Integer MONGO_PORT = 27017;
    private static final int CONFIG_SERVICE_PORT = 8888;

    @ClassRule
    public static GenericContainer mongo = new GenericContainer(MONGO_CONTAINER).withExposedPorts(MONGO_PORT);

    @ClassRule
    public static DockerComposeContainer configService =
            new DockerComposeContainer(new File("src/test/resources/configservice/docker-compose.yml"))
                    .withExposedService("ConfigService", CONFIG_SERVICE_PORT);


}

ConfigService 基本上是SpringCloud配置服务,它依赖于测试中的服务。

我的docker-compse.yaml

configservice:
  image: ponte/maven-3.5.4-jdk-11-slim-non-root
  ports:
    - "8888:8888"
  volumes:
    - "~/.m2/repository/app/configservice/configservice/0.0.1-SNAPSHOT/configservice-0.0.1-SNAPSHOT.jar:/configservice.jar"
  environment:
    - JAVA_OPTS=""
  hostname: "configservice"
  entrypoint: [ "sh", "-c", "java $JAVA_OPTS -jar /configservice.jar" ]

我的考试班

public class RepoTest extends AbstractIntegrationTest {

    @Autowired
    private MongoRepo repo;


    @Test
    public void test1() {
        assertNotNull(panoramaPostRepo);

        var post = Post.builder()
                .originalFileName("somefile.jpg")
                .dimension(new Dimension(1200, 480))
                .size(23000L)
                .uploadTime(ZonedDateTime.now())
                .build();


        var savedPost = repo.save(post);
        assertNotNull(savedPost);
        assertNotNull(savedPost.getId());
    }
}

我遇到以下错误:

org.testcontainers.containers.ContainerLaunchException: Container startup failed

    at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:229)
    at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:207)
    at org.testcontainers.containers.DockerComposeContainer.startAmbassadorContainers(DockerComposeContainer.java:249)
    at org.testcontainers.containers.DockerComposeContainer.start(DockerComposeContainer.java:159)
    at org.testcontainers.containers.DockerComposeContainer.starting(DockerComposeContainer.java:127)
    at org.testcontainers.containers.FailureDetectingExternalResource$1.evaluate(FailureDetectingExternalResource.java:29)
    at org.testcontainers.containers.FailureDetectingExternalResource$1.evaluate(FailureDetectingExternalResource.java:30)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.rnorth.ducttape.RetryCountExceededException: Retry limit hit with exception
    at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:83)
    at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:222)
    ... 14 more
Caused by: java.lang.NullPointerException: containerId was not specified
    at org.testcontainers.shaded.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:228)
    at com.github.dockerjava.core.command.LogContainerCmdImpl.withContainerId(LogContainerCmdImpl.java:78)
    at com.github.dockerjava.core.command.LogContainerCmdImpl.<init>(LogContainerCmdImpl.java:38)
    at com.github.dockerjava.core.DockerClientImpl.logContainerCmd(DockerClientImpl.java:359)
    at org.testcontainers.dockerclient.AuditLoggingDockerClient.logContainerCmd(AuditLoggingDockerClient.java:22)
    at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:285)
    at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:224)
    at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:76)
    ... 15 more

我还想知道我在做什么?

1 个答案:

答案 0 :(得分:0)

在您的docker-compose.yaml中,您有configservice,但稍后在代码中:.withExposedService("ConfigService", ...)

它区分大小写。