如果测试容器在本地机器上不可用,则 Spring Boot 测试失败

Springboot test fail if testcontainer is not available on local machine

我用的是springboot版本1.5.18.RELEASE(不过也感觉和springboot版本无关)

我正在运行在本地机器上进行 springboot 集成测试。

我在 pom.xml 中有 testcontainer 依赖,如下所示

<dependency>
 <groupId>org.testcontainers</groupId>
 <artifactId>postgresql</artifactId>
 <version>1.12.2</version>
 <scope>test</scope>
</dependency>

并且我在我的测试中添加了以下注释 class

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@AutoConfigureEmbeddedDatabase

class MyTest{}

现在当我 运行 这个 class 我得到以下异常。

java.util.concurrent.CompletionException: com.google.common.util.concurrent.UncheckedExecutionException: org.testcontainers.containers.ContainerLaunchException: Container startup failed
    at io.zonky.test.db.flyway.DefaultFlywayDataSourceContext.lambda$reload[=12=](DefaultFlywayDataSourceContext.java:113)
    at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:616)
    at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:591)
    at java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:456)
    at java.util.concurrent.CompletableFuture$UniCompletion.claim(CompletableFuture.java:543)
    at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:613)
    at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:591)
    at java.util.concurrent.CompletableFuture.uniApplyStage(CompletableFuture.java:631)
    at java.util.concurrent.CompletableFuture.thenApplyAsync(CompletableFuture.java:2006)
    at io.zonky.test.db.flyway.DefaultFlywayDataSourceContext.reload(DefaultFlywayDataSourceContext.java:106)
    at io.zonky.test.db.postgres.FlywayEmbeddedPostgresDataSourceFactoryBean.postProcessBeforeInitialization(FlywayEmbeddedPostgresDataSourceFactoryBean.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1623)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1080)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:121)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: com.google.common.util.concurrent.UncheckedExecutionException: org.testcontainers.containers.ContainerLaunchException: Container startup failed
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2214)
    at com.google.common.cache.LocalCache.get(LocalCache.java:4053)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:4057)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4986)
    at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4992)
    at io.zonky.test.db.provider.impl.DockerPostgresDatabaseProvider.getDatabase(DockerPostgresDatabaseProvider.java:92)
    at io.zonky.test.db.provider.impl.PrefetchingDatabaseProvider$PrefetchingTask.lambda$new[=12=](PrefetchingDatabaseProvider.java:252)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at io.zonky.test.db.provider.impl.PrefetchingDatabaseProvider$PrefetchingTask.run(PrefetchingDatabaseProvider.java:259)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.testcontainers.containers.ContainerLaunchException: Container startup failed
    at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:322)
    at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:302)
    at io.zonky.test.db.provider.impl.DockerPostgresDatabaseProvider$DatabaseInstance.<init>(DockerPostgresDatabaseProvider.java:155)
    at io.zonky.test.db.provider.impl.DockerPostgresDatabaseProvider$DatabaseInstance.<init>(DockerPostgresDatabaseProvider.java:111)
    at io.zonky.test.db.provider.impl.DockerPostgresDatabaseProvider.load(DockerPostgresDatabaseProvider.java:60)
    at io.zonky.test.db.provider.impl.DockerPostgresDatabaseProvider.load(DockerPostgresDatabaseProvider.java:58)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3628)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2336)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2295)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2208)
    ... 11 common frames omitted
Caused by: org.testcontainers.containers.ContainerFetchException: Can't get Docker image: RemoteDockerImage(imageNameFuture=java.util.concurrent.CompletableFuture@7b155ed5[Completed normally], imagePullPolicy=DefaultPullPolicy(), dockerClient=LazyDockerClient.INSTANCE)
    at org.testcontainers.containers.GenericContainer.getDockerImageName(GenericContainer.java:1265)
    at org.testcontainers.containers.GenericContainer.logger(GenericContainer.java:600)
    at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:311)
    ... 20 common frames omitted
Caused by: com.github.dockerjava.api.exception.NotFoundException: {"message":"No such image: quay.io/testcontainers/ryuk:0.2.3"}

    at org.testcontainers.dockerclient.transport.okhttp.OkHttpInvocationBuilder.execute(OkHttpInvocationBuilder.java:281)
    at org.testcontainers.dockerclient.transport.okhttp.OkHttpInvocationBuilder.execute(OkHttpInvocationBuilder.java:265)
    at org.testcontainers.dockerclient.transport.okhttp.OkHttpInvocationBuilder.post(OkHttpInvocationBuilder.java:136)
    at com.github.dockerjava.core.exec.CreateContainerCmdExec.execute(CreateContainerCmdExec.java:33)
    at com.github.dockerjava.core.exec.CreateContainerCmdExec.execute(CreateContainerCmdExec.java:13)
    at com.github.dockerjava.core.exec.AbstrSyncDockerCmdExec.exec(AbstrSyncDockerCmdExec.java:21)
    at com.github.dockerjava.core.command.AbstrDockerCmd.exec(AbstrDockerCmd.java:35)
    at com.github.dockerjava.core.command.CreateContainerCmdImpl.exec(CreateContainerCmdImpl.java:1139)
    at org.testcontainers.utility.ResourceReaper.start(ResourceReaper.java:85)
    at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:155)
    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:62)
    at org.testcontainers.images.RemoteDockerImage.resolve(RemoteDockerImage.java:25)
    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:1263)
    ... 22 common frames omitted

这个异常只有一行

Caused by: com.github.dockerjava.api.exception.NotFoundException: {"message":"No such image: quay.io/testcontainers/ryuk:0.2.3"}

如果我在我的本地机器上手动拉取这个镜像然后这个测试就通过了。

我觉得 springboot test 运行 在这个镜像从他们的服务器上拉出来之前

我该如何解决这个问题?

更新 1:

根据@Michael McFadyen的建议,我将版本从 1.12.2 更改为 1.15.1,现在我遇到了新的错误

2021-07-17;17:39:08.117;prefetching-1;ERROR;o.t.u.ResourceReaper;Timed out waiting for Ryuk container to start. Ryuk's logs:
2021/07/17 17:38:37 Pinging Docker...
2021/07/17 17:38:37 Docker daemon is available!
2021/07/17 17:38:37 Starting on port 8080...
2021/07/17 17:38:37 Started!

2021-07-17;17:39:08.241;main;ERROR;i.z.t.d.p.FlywayEmbeddedPostgresDataSourceFactoryBean;Unexpected error during the initialization of embedded database
java.util.concurrent.CompletionException: com.google.common.util.concurrent.UncheckedExecutionException: java.lang.IllegalStateException: Could not connect to Ryuk at localhost:49154
    at io.zonky.test.db.flyway.DefaultFlywayDataSourceContext.lambda$reload[=14=](DefaultFlywayDataSourceContext.java:113)
    at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:602)
    at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
    at java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:442)
    at java.util.concurrent.CompletableFuture$UniCompletion.claim(CompletableFuture.java:529)
    at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:599)
    at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
    at java.util.concurrent.CompletableFuture.uniApplyStage(CompletableFuture.java:617)
    at java.util.concurrent.CompletableFuture.thenApplyAsync(CompletableFuture.java:1993)
    at io.zonky.test.db.flyway.DefaultFlywayDataSourceContext.reload(DefaultFlywayDataSourceContext.java:106)
    at io.zonky.test.db.postgres.FlywayEmbeddedPostgresDataSourceFactoryBean.postProcessBeforeInitialization(FlywayEmbeddedPostgresDataSourceFactoryBean.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1623)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1080)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:121)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:383)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:344)
    at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:417)
Caused by: com.google.common.util.concurrent.UncheckedExecutionException: java.lang.IllegalStateException: Could not connect to Ryuk at localhost:49154
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2214)
    at com.google.common.cache.LocalCache.get(LocalCache.java:4053)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:4057)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4986)
    at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4992)
    at io.zonky.test.db.provider.impl.DockerPostgresDatabaseProvider.getDatabase(DockerPostgresDatabaseProvider.java:92)
    at io.zonky.test.db.provider.impl.PrefetchingDatabaseProvider$PrefetchingTask.lambda$new[=14=](PrefetchingDatabaseProvider.java:252)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at io.zonky.test.db.provider.impl.PrefetchingDatabaseProvider$PrefetchingTask.run(PrefetchingDatabaseProvider.java:259)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Could not connect to Ryuk at localhost:49154
    at org.testcontainers.utility.ResourceReaper.start(ResourceReaper.java:198)
    at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:205)
    at org.testcontainers.LazyDockerClient.getDockerClient(LazyDockerClient.java:14)
    at org.testcontainers.LazyDockerClient.authConfig(LazyDockerClient.java:12)
    at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:310)
    at io.zonky.test.db.provider.impl.DockerPostgresDatabaseProvider$DatabaseInstance.<init>(DockerPostgresDatabaseProvider.java:155)
    at io.zonky.test.db.provider.impl.DockerPostgresDatabaseProvider$DatabaseInstance.<init>(DockerPostgresDatabaseProvider.java:111)
    at io.zonky.test.db.provider.impl.DockerPostgresDatabaseProvider.load(DockerPostgresDatabaseProvider.java:60)
    at io.zonky.test.db.provider.impl.DockerPostgresDatabaseProvider.load(DockerPostgresDatabaseProvider.java:58)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3628)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2336)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2295)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2208)
    ... 11 common frames omitted

这是测试容器库中的一个 issue,是由于使用了在更高版本的 docker api 中删除的查询参数。 1.15.1 中有一个修复程序。你能试试升级到那个版本吗?

有关详细信息,请阅读链接的 github 问题。