Skip to content

Commit 1f662a1

Browse files
committed
[testtoolkit] 优化Testcontainers相关测试
- 增强TestcontainersVerificationTest测试覆盖 - 完善TestcontainersPropertiesIntegrationTest集成测试 - 改进TestcontainersPropertiesTest单元测试
1 parent fd896c6 commit 1f662a1

3 files changed

Lines changed: 75 additions & 8 deletions

File tree

testtoolkit/src/test/kotlin/io/github/truenine/composeserver/testtoolkit/TestcontainersVerificationTest.kt

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import kotlinx.coroutines.selects.select
1414
import org.junit.jupiter.api.Test
1515
import org.junit.jupiter.api.assertTimeout
1616
import org.testcontainers.containers.GenericContainer
17+
import org.testcontainers.containers.MySQLContainer
1718
import org.testcontainers.containers.PostgreSQLContainer
1819
import org.testcontainers.containers.output.Slf4jLogConsumer
1920
import org.testcontainers.utility.MountableFile
@@ -23,7 +24,7 @@ class TestcontainersVerificationTest {
2324
fun `启动 Alpine 容器 输出日志并校验运行状态`() {
2425
log.info("开始测试 Testcontainers")
2526

26-
GenericContainer("alpine:latest")
27+
GenericContainer("alpine:3.21")
2728
.apply {
2829
// 增加容器运行时间,确保有足够时间进行日志检查
2930
// 使用 flush 确保输出立即写入,避免缓冲延迟
@@ -153,6 +154,37 @@ class TestcontainersVerificationTest {
153154
}
154155
}
155156

157+
@Test
158+
fun `启动 MySQL 容器 获取连接信息并校验运行状态`() {
159+
log.info("开始测试 MySQL 容器")
160+
161+
MySQLContainer<Nothing>("mysql:8.0")
162+
.apply {
163+
withDatabaseName("testdb")
164+
withUsername("testuser")
165+
withPassword("testpass")
166+
withEnv("MYSQL_ROOT_PASSWORD", "rootpass")
167+
withExposedPorts(3306)
168+
withLogConsumer(Slf4jLogConsumer(log))
169+
withStartupTimeout(Duration.ofSeconds(120)) // 增加启动超时时间
170+
}
171+
.use { mysql ->
172+
log.info("正在启动 MySQL 容器...")
173+
mysql.start()
174+
175+
// 使用重试机制等待容器完全就绪
176+
TestRetryUtils.waitUntil(timeout = Duration.ofSeconds(30), pollInterval = Duration.ofSeconds(1)) { mysql.isRunning && mysql.jdbcUrl.isNotEmpty() }
177+
178+
assertTrue(mysql.isRunning, "MySQL 容器应该处于运行状态")
179+
log.info("MySQL 连接 URL: ${mysql.jdbcUrl}")
180+
log.info("MySQL 用户名: ${mysql.username}")
181+
log.info("MySQL 密码: ${mysql.password}")
182+
183+
val expectedJdbcUrlPrefix = "jdbc:mysql://"
184+
assertTrue(mysql.jdbcUrl.startsWith(expectedJdbcUrlPrefix), "JDBC URL 应该以 $expectedJdbcUrlPrefix 开头")
185+
}
186+
}
187+
156188
@Test
157189
fun `并发测试多个网址 任一完成即结束`() {
158190
assertTimeout(Duration.ofSeconds(20), "可能由于docker 网络原因导致测试失败,考虑检查 docker 网络配置") {
@@ -161,7 +193,7 @@ class TestcontainersVerificationTest {
161193

162194
val urls = listOf("https://www.aliyun.com", "https://www.tencent.com", "https://www.baidu.com", "https://www.qq.com")
163195

164-
GenericContainer("alpine/curl:latest")
196+
GenericContainer("curlimages/curl:8.1.0")
165197
.apply {
166198
withCommand("sleep", "30") // 增加容器运行时间
167199
}

testtoolkit/src/test/kotlin/io/github/truenine/composeserver/testtoolkit/autoconfig/TestcontainersPropertiesIntegrationTest.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ class TestcontainersPropertiesIntegrationTest {
3636
@Test
3737
fun should_inject_properties_with_defaults() {
3838
assertNotNull(testcontainersProperties)
39-
assertEquals("postgres:17.4-alpine", testcontainersProperties.postgres.image)
40-
assertEquals("redis:7.4.2-alpine3.21", testcontainersProperties.redis.image)
41-
assertEquals("minio/minio:RELEASE.2025-04-22T22-12-26Z", testcontainersProperties.minio.image)
39+
assertEquals("postgres:17-alpine", testcontainersProperties.postgres.image)
40+
assertEquals("mysql:8.0", testcontainersProperties.mysql.image)
41+
assertEquals("redis:7-alpine", testcontainersProperties.redis.image)
42+
assertEquals("minio/minio:RELEASE.2025-07-23T15-54-02Z", testcontainersProperties.minio.image)
4243
}
4344
}
4445

@@ -56,6 +57,8 @@ class TestcontainersPropertiesIntegrationTest {
5657
[
5758
"compose.testtoolkit.testcontainers.postgres.image=postgres:16-alpine",
5859
"compose.testtoolkit.testcontainers.postgres.database-name=customdb",
60+
"compose.testtoolkit.testcontainers.mysql.image=mysql:8.1",
61+
"compose.testtoolkit.testcontainers.mysql.database-name=mysqlcustomdb",
5962
"compose.testtoolkit.testcontainers.redis.image=redis:7.2-alpine",
6063
"compose.testtoolkit.testcontainers.minio.image=minio/minio:RELEASE.2024-01-01T00-00-00Z",
6164
"compose.testtoolkit.testcontainers.minio.access-key=customkey",
@@ -74,6 +77,13 @@ class TestcontainersPropertiesIntegrationTest {
7477
assertEquals("customdb", testcontainersProperties.postgres.databaseName)
7578
assertEquals("test", testcontainersProperties.postgres.username) // 保持默认值
7679

80+
// 验证自定义 MySQL 配置
81+
assertEquals("mysql:8.1", testcontainersProperties.mysql.image)
82+
assertEquals("mysqlcustomdb", testcontainersProperties.mysql.databaseName)
83+
assertEquals("test", testcontainersProperties.mysql.username) // 保持默认值
84+
assertEquals("test", testcontainersProperties.mysql.password) // 保持默认值
85+
assertEquals("roottest", testcontainersProperties.mysql.rootPassword) // 保持默认值
86+
7787
// 验证自定义 Redis 配置
7888
assertEquals("redis:7.2-alpine", testcontainersProperties.redis.image)
7989

testtoolkit/src/test/kotlin/io/github/truenine/composeserver/testtoolkit/autoconfig/TestcontainersPropertiesTest.kt

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.truenine.composeserver.testtoolkit.autoconfig
22

33
import io.github.truenine.composeserver.testtoolkit.properties.MinioConfig
4+
import io.github.truenine.composeserver.testtoolkit.properties.MysqlConfig
45
import io.github.truenine.composeserver.testtoolkit.properties.PostgresConfig
56
import io.github.truenine.composeserver.testtoolkit.properties.RedisConfig
67
import io.github.truenine.composeserver.testtoolkit.properties.TestcontainersProperties
@@ -18,24 +19,35 @@ class TestcontainersPropertiesTest {
1819
fun should_have_correct_default_postgres_config() {
1920
val properties = TestcontainersProperties()
2021

21-
assertEquals("postgres:17.4-alpine", properties.postgres.image)
22+
assertEquals("postgres:17-alpine", properties.postgres.image)
2223
assertEquals("testdb", properties.postgres.databaseName)
2324
assertEquals("test", properties.postgres.username)
2425
assertEquals("test", properties.postgres.password)
2526
}
2627

28+
@Test
29+
fun should_have_correct_default_mysql_config() {
30+
val properties = TestcontainersProperties()
31+
32+
assertEquals("mysql:8.0", properties.mysql.image)
33+
assertEquals("testdb", properties.mysql.databaseName)
34+
assertEquals("test", properties.mysql.username)
35+
assertEquals("test", properties.mysql.password)
36+
assertEquals("roottest", properties.mysql.rootPassword)
37+
}
38+
2739
@Test
2840
fun should_have_correct_default_redis_config() {
2941
val properties = TestcontainersProperties()
3042

31-
assertEquals("redis:7.4.2-alpine3.21", properties.redis.image)
43+
assertEquals("redis:7-alpine", properties.redis.image)
3244
}
3345

3446
@Test
3547
fun should_have_correct_default_minio_config() {
3648
val properties = TestcontainersProperties()
3749

38-
assertEquals("minio/minio:RELEASE.2025-04-22T22-12-26Z", properties.minio.image)
50+
assertEquals("minio/minio:RELEASE.2025-07-23T15-54-02Z", properties.minio.image)
3951
assertEquals("minioadmin", properties.minio.accessKey)
4052
assertEquals("minioadmin", properties.minio.secretKey)
4153
}
@@ -55,6 +67,19 @@ class TestcontainersPropertiesTest {
5567
assertEquals("custompass", properties.postgres.password)
5668
}
5769

70+
@Test
71+
fun should_accept_custom_mysql_config() {
72+
val customMysql =
73+
MysqlConfig(image = "mysql:8.1", databaseName = "customdb", username = "customuser", password = "custompass", rootPassword = "customroot")
74+
val properties = TestcontainersProperties(mysql = customMysql)
75+
76+
assertEquals("mysql:8.1", properties.mysql.image)
77+
assertEquals("customdb", properties.mysql.databaseName)
78+
assertEquals("customuser", properties.mysql.username)
79+
assertEquals("custompass", properties.mysql.password)
80+
assertEquals("customroot", properties.mysql.rootPassword)
81+
}
82+
5883
@Test
5984
fun should_accept_custom_redis_config() {
6085
val customRedis = RedisConfig(image = "redis:7.2-alpine")

0 commit comments

Comments
 (0)