【发布时间】:2021-10-10 01:54:42
【问题描述】:
我在我的 oss 软件中使用了 testcontainer,但我认为我的配置或 docker/testcontainer 运行时存在问题...
我有一些测试,当它们分开运行时,一切正常,但是当我尝试运行所有测试时,由于应用程序尝试与容器连接时出现问题,最后一次失败..
调试问题我发现容器在一个端口启动,但应用程序正在尝试在另一个端口连接,大部分都是在上次运行的测试类中使用
所有测试正在运行:
其中一个失败的测试向我显示了这个日志:
当 UserControllerTest 启动的类使用另一个端口时,容器启动,如下所示:
docker on windows showing the container port
我的测试配置是基于一个抽象类(见下文),就像上面所说的,如果单独运行显示错误的类,一切正常。
@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)
}
}
}
有人见过这样的事情知道如何解决吗?
【问题讨论】:
标签: spring-boot docker kotlin testcontainers testcontainers-junit5