Skip to content

Commit 513c4ab

Browse files
committed
[testtoolkit] 新增MySQL测试容器支持
- 添加IDatabaseMysqlContainer接口 - 扩展TestcontainersProperties支持MySQL配置 - 完善MySQL容器使用文档和注释
1 parent e07f425 commit 513c4ab

2 files changed

Lines changed: 112 additions & 8 deletions

File tree

testtoolkit/src/main/kotlin/io/github/truenine/composeserver/testtoolkit/properties/TestcontainersProperties.kt

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ import org.springframework.boot.context.properties.ConfigurationProperties
1414
* testtoolkit:
1515
* testcontainers:
1616
* postgres:
17-
* image: "postgres:17.4-alpine"
17+
* image: "postgres:17-alpine"
18+
* mysql:
19+
* image: "mysql:8.0"
1820
* redis:
19-
* image: "redis:7.4.2-alpine3.21"
21+
* image: "redis:7-alpine"
2022
* minio:
21-
* image: "minio/minio:RELEASE.2025-04-22T22-12-26Z"
23+
* image: "minio/minio:RELEASE.2025-07-23T15-54-02Z"
2224
* ```
2325
*
2426
* @author TrueNine
@@ -29,6 +31,9 @@ data class TestcontainersProperties(
2931
/** PostgreSQL 配置 */
3032
val postgres: PostgresConfig = PostgresConfig(),
3133

34+
/** MySQL 配置 */
35+
val mysql: MysqlConfig = MysqlConfig(),
36+
3237
/** Redis 配置 */
3338
val redis: RedisConfig = RedisConfig(),
3439

@@ -38,8 +43,8 @@ data class TestcontainersProperties(
3843

3944
/** # PostgreSQL 容器配置 */
4045
data class PostgresConfig(
41-
/** PostgreSQL Docker 镜像 默认使用 postgres:17.4-alpine(当前 LTS 版本) */
42-
val image: String = "postgres:17.4-alpine",
46+
/** PostgreSQL Docker 镜像 默认使用 postgres:17-alpine(当前 LTS 版本) */
47+
val image: String = "postgres:17-alpine",
4348

4449
/** 默认数据库名称 */
4550
val databaseName: String = "testdb",
@@ -51,16 +56,34 @@ data class PostgresConfig(
5156
val password: String = "test",
5257
)
5358

59+
/** # MySQL 容器配置 */
60+
data class MysqlConfig(
61+
/** MySQL Docker 镜像 默认使用 mysql:8.0(当前 LTS 版本) */
62+
val image: String = "mysql:8.0",
63+
64+
/** 默认数据库名称 */
65+
val databaseName: String = "testdb",
66+
67+
/** 默认用户名 */
68+
val username: String = "test",
69+
70+
/** 默认密码 */
71+
val password: String = "test",
72+
73+
/** 默认根密码 */
74+
val rootPassword: String = "roottest",
75+
)
76+
5477
/** # Redis 容器配置 */
5578
data class RedisConfig(
56-
/** Redis Docker 镜像 默认使用 redis:7.4.2-alpine3.21(当前稳定版本) */
57-
val image: String = "redis:7.4.2-alpine3.21"
79+
/** Redis Docker 镜像 默认使用 redis:7-alpine(当前稳定版本) */
80+
val image: String = "redis:7-alpine"
5881
)
5982

6083
/** # MinIO 容器配置 */
6184
data class MinioConfig(
6285
/** MinIO Docker 镜像 默认使用较新的稳定版本 */
63-
val image: String = "minio/minio:RELEASE.2025-04-22T22-12-26Z",
86+
val image: String = "minio/minio:RELEASE.2025-07-23T15-54-02Z",
6487

6588
/** 默认访问密钥 */
6689
val accessKey: String = "minioadmin",
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package io.github.truenine.composeserver.testtoolkit.testcontainers
2+
3+
import org.springframework.test.context.DynamicPropertyRegistry
4+
import org.springframework.test.context.DynamicPropertySource
5+
import org.testcontainers.containers.MySQLContainer
6+
import org.testcontainers.junit.jupiter.Testcontainers
7+
8+
/**
9+
* # MySQL 数据库测试容器接口
10+
*
11+
* 该接口提供了MySQL测试容器的标准配置,用于集成测试环境。 通过实现此接口,测试类可以自动获得配置好的MySQL测试数据库实例。
12+
*
13+
* ## 特性
14+
* - 自动配置MySQL测试容器
15+
* - 提供标准的数据库连接配置
16+
* - 支持Spring Test的动态属性注入
17+
*
18+
* ## 使用方式
19+
*
20+
* ```kotlin
21+
* @SpringBootTest
22+
* class YourTestClass : IDatabaseMysqlContainer {
23+
* // 你的测试代码
24+
* }
25+
* ```
26+
*
27+
* @see org.testcontainers.junit.jupiter.Testcontainers
28+
* @see org.testcontainers.containers.MySQLContainer
29+
* @author TrueNine
30+
* @since 2025-07-28
31+
*/
32+
@Testcontainers
33+
interface IDatabaseMysqlContainer {
34+
companion object {
35+
/**
36+
* MySQL 测试容器实例
37+
*
38+
* 预配置的 MySQL 容器,设置可通过配置自定义:
39+
* - 数据库名称: 可配置,默认 testdb
40+
* - 用户名: 可配置,默认 test
41+
* - 密码: 可配置,默认 test
42+
* - 根密码: 可配置,默认 roottest
43+
* - 版本: 可配置,默认 mysql:8.0
44+
*/
45+
@JvmStatic
46+
val container by lazy {
47+
val config = TestcontainersConfigurationHolder.getTestcontainersProperties()
48+
MySQLContainer<Nothing>(config.mysql.image).apply {
49+
withDatabaseName(config.mysql.databaseName)
50+
withUsername(config.mysql.username)
51+
withPassword(config.mysql.password)
52+
withEnv("MYSQL_ROOT_PASSWORD", config.mysql.rootPassword)
53+
addExposedPorts(3306)
54+
start()
55+
}
56+
}
57+
58+
/**
59+
* Spring测试环境动态属性配置
60+
*
61+
* 自动注入数据库连接相关的配置属性到Spring测试环境中:
62+
* - JDBC URL
63+
* - 用户名
64+
* - 密码
65+
* - 数据库驱动类名
66+
*
67+
* @param registry Spring动态属性注册器
68+
*/
69+
@JvmStatic
70+
@DynamicPropertySource
71+
fun properties(registry: DynamicPropertyRegistry) {
72+
registry.add("spring.datasource.url", container::getJdbcUrl)
73+
registry.add("spring.datasource.username", container::getUsername)
74+
registry.add("spring.datasource.password", container::getPassword)
75+
registry.add("spring.datasource.driver-class-name") { "com.mysql.cj.jdbc.Driver" }
76+
}
77+
}
78+
79+
val mysqlContainer: MySQLContainer<*>?
80+
get() = container
81+
}

0 commit comments

Comments
 (0)