首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行测试套件时的测试容器initializationError

运行测试套件时的测试容器initializationError
EN

Stack Overflow用户
提问于 2021-02-18 19:33:56
回答 1查看 2.7K关注 0票数 2

我有多个测试类运行相同的docker-compose

测试容器

..。

该套件失败,出现以下错误

尽管每个测试在单独执行时都会通过。

这是在第二个测试期间发生的堆栈跟踪的相关部分。

代码语言:javascript
复制
io.foo.e2e.AuthTest > initializationError FAILED
    org.testcontainers.containers.ContainerLaunchException: Container startup failed
        at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:330)
        at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:311)
        at org.testcontainers.containers.DockerComposeContainer.startAmbassadorContainers(DockerComposeContainer.java:331)
        at org.testcontainers.containers.DockerComposeContainer.start(DockerComposeContainer.java:178)
        at io.foo.e2e.bases.BaseE2eTest$Companion.beforeAll$e2e(BaseE2eTest.kt:62)
        at io.foo.e2e.bases.BaseE2eTest.beforeAll$e2e(BaseE2eTest.kt)
       ...

        Caused by:
        org.rnorth.ducttape.RetryCountExceededException: Retry limit hit with exception
            at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:88)
            at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:323)
            ... 83 more

            Caused by:
            org.testcontainers.containers.ContainerLaunchException: Could not create/start container
                at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:497)
                at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:325)
                at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:81)
                ... 84 more

                Caused by:
                org.testcontainers.containers.ContainerLaunchException: Aborting attempt to link to container btraq5fzahac_worker_1 as it is not running
                    at org.testcontainers.containers.GenericContainer.applyConfiguration(GenericContainer.java:779)
                    at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:359)
                    ... 86 more

在我看来,第二次测试并没有等到第一次关闭之前的容器。

这里是所有测试继承的基类。它负责旋转容器。

代码语言:javascript
复制
open class BaseE2eTest {

    ...

    companion object {
        const val A = "containera_1"
        const val B = "containerb_1"
        const val C = "containerc_1"

        val dockerCompose: KDockerComposeContainer by lazy {
            defineDockerCompose()
                .withLocalCompose(true)
                .withExposedService(A, 8080, Wait.forListeningPort())
                .withExposedService(B, 8081)
                .withExposedService(C, 5672, Wait.forListeningPort())
        }

        class KDockerComposeContainer(file: File) : DockerComposeContainer(file)

        private fun defineDockerCompose() = KDockerComposeContainer(File("../docker-compose.yml"))

        @BeforeAll
        @JvmStatic
        internal fun beforeAll() {
            dockerCompose.start()
        }

        @AfterAll
        @JvmStatic
        internal fun afterAll() {
            dockerCompose.stop()
        }
    }
}
代码语言:javascript
复制
docker-compose version 1.27.4, build 40524192
testcontainer 1.15.2
testcontainers:junit-jupiter:1.15.2
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-01 07:15:13

看完之后

此演讲

,我意识到我使用Junit5实例化测试容器的方法是错误的。

以下是工作代码:

代码语言:javascript
复制
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
open class BaseE2eTest {

    ...

    val A = "containera_1"
    val B = "containerb_1"
    val C = "containerc_1"

    val dockerCompose: KDockerComposeContainer by lazy {
        defineDockerCompose()
            .withLocalCompose(true)
            .withExposedService(A, 8080, Wait.forListeningPort())
            .withExposedService(B, 8081)
            .withExposedService(C, 5672, Wait.forListeningPort())
    }

    class KDockerComposeContainer(file: File) : DockerComposeContainer(file)

    private fun defineDockerCompose() = KDockerComposeContainer(File("../docker-compose.yml"))

    @BeforeAll
    fun beforeAll() {
        dockerCompose.start()
    }

    @AfterAll
    fun afterAll() {
        dockerCompose.stop()
    }
}

现在测试套件通过了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66259110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档