小编Art*_*hur的帖子

运行一些测试后测试容器失去连接

我在我的oss软件中使用testcontainer,但我认为我的配置或docker/testcontainer运行时有问题......

我有一些测试,当它们分开运行时,一切正常,但是当我尝试运行所有测试时,最后一个测试由于应用程序尝试与容器连接时出现问题而失败。

调试问题我发现容器在一个端口启动,但应用程序正在尝试在其他端口连接,其中大部分在最后运行的测试类中使用

所有测试正在运行:

测试失败

失败的测试之一向我显示了以下日志:

失败测试的日志

当类启动时容器启动UserControllerTest使用另一个端口,如下所示:

Windows 上的 docker 显示容器端口

我的测试配置基于一个抽象类(见下文),就像所说的那样,如果运行仅显示错误的类,则一切正常。

@Testcontainers
@ActiveProfiles("test")
@ExtendWith(SpringExtension::class)
@TestMethodOrder(value = OrderAnnotation::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
abstract class AbstractTest {

    companion object {

        @Container
        private val redisContainer = GenericContainer<Nothing>("redis:6-alpine")
            .apply {
                withExposedPorts(6379)
                withCreateContainerCmdModifier { cmd -> cmd.withName("wb-test-cache") }
            }

        @Container
        private val postgresContainer = PostgreSQLContainer<Nothing>("postgres:13-alpine")
            .apply {
                withExposedPorts(5432)
                withUsername("sa_webbudget")
                withPassword("sa_webbudget")
                withDatabaseName("webbudget")
                withCreateContainerCmdModifier { cmd -> cmd.withName("wb-test-database") }
            }

        @JvmStatic
        @DynamicPropertySource
        fun dynamicPropertiesRegister(registry: DynamicPropertyRegistry) {
            registry.add("spring.datasource.url", postgresContainer::getJdbcUrl)
            registry.add("spring.redis.host", redisContainer::getHost)
            registry.add("spring.redis.port", redisContainer::getFirstMappedPort)
        }
    } …
Run Code Online (Sandbox Code Playgroud)

kotlin docker spring-boot testcontainers testcontainers-junit5

4
推荐指数
1
解决办法
3302
查看次数