我在我的oss软件中使用testcontainer,但我认为我的配置或docker/testcontainer运行时有问题......
我有一些测试,当它们分开运行时,一切正常,但是当我尝试运行所有测试时,最后一个测试由于应用程序尝试与容器连接时出现问题而失败。
调试问题我发现容器在一个端口启动,但应用程序正在尝试在其他端口连接,其中大部分在最后运行的测试类中使用
所有测试正在运行:
失败的测试之一向我显示了以下日志:
当类启动时容器启动UserControllerTest使用另一个端口,如下所示:
我的测试配置基于一个抽象类(见下文),就像所说的那样,如果运行仅显示错误的类,则一切正常。
@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