Junit 5 集成测试 Docker

Junit 5 Integration Test Docker

我正在尝试 运行 创建 InfluxDB docker 实例的集成测试。我正在使用 Junit5-docker。我在文档中没有看到任何关于指定容器 ID 的内容。据我所知,该示例完全符合我的要求。有谁知道我做错了什么?

这是测试文件:

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.github.junit5docker.Docker;
import com.github.junit5docker.Port;
import com.github.junit5docker.WaitFor;
import com.******.healthmonitor.HealthMonitorSvcApplication;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;

@Profile("medium-test")
@SpringBootTest(classes = HealthMonitorSvcApplication.class)
@Docker(image = "influxdb", ports = @Port(exposed = 8086, inner = 8086), waitFor = @WaitFor("Listening on HTTP"))
class MediumTests {

    @BeforeEach
    void setup() {

    }

    @Test
    void endToEndTest() {
        InfluxDB db = InfluxDBFactory.connect("http://localhost:8086", "root", "root");
        db.ping();
        assertEquals(true, true);
    }
}

我运行 endToEndTest 时的输出是:

08:56:00.754 [main] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: influxdb:latest,com.github.dockerjava.jaxrs.InspectImageCmdExec@5aebe890

java.lang.NoSuchMethodError: javax.ws.rs.core.UriBuilder.resolveTemplate(Ljava/lang/String;Ljava/lang/Object;Z)Ljavax/ws/rs/core/UriBuilder;

    at org.glassfish.jersey.client.JerseyWebTarget.resolveTemplate(JerseyWebTarget.java:244)
    at org.glassfish.jersey.client.JerseyWebTarget.resolveTemplate(JerseyWebTarget.java:235)
    at org.glassfish.jersey.client.JerseyWebTarget.resolveTemplate(JerseyWebTarget.java:59)
    at com.github.dockerjava.jaxrs.InspectImageCmdExec.execute(InspectImageCmdExec.java:24)
    at com.github.dockerjava.jaxrs.InspectImageCmdExec.execute(InspectImageCmdExec.java:13)
    at com.github.dockerjava.jaxrs.AbstrSyncDockerCmdExec.exec(AbstrSyncDockerCmdExec.java:23)
    at com.github.dockerjava.core.command.AbstrDockerCmd.exec(AbstrDockerCmd.java:35)
    at com.github.dockerjava.core.command.InspectImageCmdImpl.exec(InspectImageCmdImpl.java:40)
    at com.github.junit5docker.DefaultDockerClient.ensureImageExists(DefaultDockerClient.java:82)
    at com.github.junit5docker.DefaultDockerClient.createContainer(DefaultDockerClient.java:56)
    at com.github.junit5docker.DefaultDockerClient.startContainer(DefaultDockerClient.java:31)
    at com.github.junit5docker.DockerExtension.startContainer(DockerExtension.java:51)
    at com.github.junit5docker.DockerExtension.beforeEach(DockerExtension.java:43)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$null[=12=](TestMethodTestDescriptor.java:126)
    at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:152)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeEachCallbacks(TestMethodTestDescriptor.java:125)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:104)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute(HierarchicalTestExecutor.java:83)
    at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null(HierarchicalTestExecutor.java:92)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
    at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:175)
    at java.util.Iterator.forEachRemaining(Iterator.java:116)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute(HierarchicalTestExecutor.java:92)
    at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null(HierarchicalTestExecutor.java:92)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
    at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:175)
    at java.util.Iterator.forEachRemaining(Iterator.java:116)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute(HierarchicalTestExecutor.java:92)
    at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:51)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:65)
    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)
    Suppressed: java.lang.NullPointerException: containerId was not specified
        at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:226)
        at com.github.dockerjava.core.command.StopContainerCmdImpl.withContainerId(StopContainerCmdImpl.java:42)
        at com.github.dockerjava.core.command.StopContainerCmdImpl.<init>(StopContainerCmdImpl.java:27)
        at com.github.dockerjava.core.DockerClientImpl.stopContainerCmd(DockerClientImpl.java:363)
        at com.github.junit5docker.DefaultDockerClient.stopAndRemoveContainer(DefaultDockerClient.java:38)
        at com.github.junit5docker.DockerExtension.afterEach(DockerExtension.java:120)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$null(TestMethodTestDescriptor.java:214)
        at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks(TestMethodTestDescriptor.java:226)
        at java.util.ArrayList.forEach(ArrayList.java:1257)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAllAfterMethodsOrCallbacks(TestMethodTestDescriptor.java:224)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAfterEachCallbacks(TestMethodTestDescriptor.java:213)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:116)
        ... 41 more


Process finished with exit code 255

看起来您有 Jersey 客户端类路径冲突。 先尝试解决。 比如看这个问题java.lang.NoSuchMethodError: javax.ws.rs.core.UriBuilder.resolveTemplate() using jaxrs cxf implementation

如果您使用 Maven 和 Intellij IDEA,则有一个 Maven Helper 插件可以帮助解决冲突: https://plugins.jetbrains.com/plugin/7179-maven-helper

java.lang.NullPointerException: containerId was not specified 似乎是 Docker 插件中的错误。但这不是你的主要问题。 从代码来看,容器似乎从未启动过,但插件仍然试图停止它: https://github.com/FaustXVI/junit5-docker/blob/242ed570e29e824bbf147eafc74d272e445461d5/src/main/java/com/github/junit5docker/DockerExtension.java#L120