Skip to content

Commit 98235d8

Browse files
committed
[testtoolkit] 重构工具类测试代码
- 优化TestcontainersConfigurationHolderTest测试结构 - 改进ContainerCommandExecutorTest测试用例 - 增强ContainerExtensionsTest测试覆盖率
1 parent 1f662a1 commit 98235d8

3 files changed

Lines changed: 34 additions & 34 deletions

File tree

testtoolkit/src/test/kotlin/io/github/truenine/composeserver/testtoolkit/testcontainers/TestcontainersConfigurationHolderTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class TestcontainersConfigurationHolderTest {
2929
assertNotNull(properties.minio, "MinIO 配置不应为 null")
3030

3131
// 验证默认配置值
32-
assertEquals("postgres:17.4-alpine", properties.postgres.image, "PostgreSQL 默认镜像应该正确")
33-
assertEquals("redis:7.4.2-alpine3.21", properties.redis.image, "Redis 默认镜像应该正确")
34-
assertEquals("minio/minio:RELEASE.2025-04-22T22-12-26Z", properties.minio.image, "MinIO 默认镜像应该正确")
32+
assertEquals("postgres:17-alpine", properties.postgres.image, "PostgreSQL 默认镜像应该正确")
33+
assertEquals("redis:7-alpine", properties.redis.image, "Redis 默认镜像应该正确")
34+
assertEquals("minio/minio:RELEASE.2025-07-23T15-54-02Z", properties.minio.image, "MinIO 默认镜像应该正确")
3535

3636
// 验证配置的完整性
3737
assertTrue(properties.postgres.image.isNotEmpty(), "PostgreSQL 镜像名不应为空")
@@ -56,7 +56,7 @@ class TestcontainersConfigurationHolderTest {
5656

5757
// 验证在Bean不存在时返回默认配置
5858
assertNotNull(properties, "属性对象不应为 null")
59-
assertEquals("postgres:17.4-alpine", properties.postgres.image, "PostgreSQL 默认镜像应该正确")
59+
assertEquals("postgres:17-alpine", properties.postgres.image, "PostgreSQL 默认镜像应该正确")
6060

6161
// 验证所有默认配置都可用
6262
assertNotNull(properties.postgres, "PostgreSQL 配置不应为 null")

testtoolkit/src/test/kotlin/io/github/truenine/composeserver/testtoolkit/utils/ContainerCommandExecutorTest.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ContainerCommandExecutorTest {
2020
fun `测试 ContainerCommandExecutor 构造函数和常量`() {
2121
log.info("开始测试 ContainerCommandExecutor 构造函数和常量")
2222

23-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
23+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
2424
container.start()
2525

2626
val executor = ContainerCommandExecutor(container)
@@ -38,7 +38,7 @@ class ContainerCommandExecutorTest {
3838
fun `测试 executeCommand 方法的成功执行`() {
3939
log.info("开始测试 executeCommand 方法的成功执行")
4040

41-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
41+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
4242
container.start()
4343

4444
val executor = ContainerCommandExecutor(container)
@@ -55,7 +55,7 @@ class ContainerCommandExecutorTest {
5555
fun `测试 executeCommand 方法的空命令验证`() {
5656
log.info("开始测试 executeCommand 方法的空命令验证")
5757

58-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
58+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
5959
container.start()
6060

6161
val executor = ContainerCommandExecutor(container)
@@ -70,7 +70,7 @@ class ContainerCommandExecutorTest {
7070
fun `测试 executeCommand 方法的自定义参数`() {
7171
log.info("开始测试 executeCommand 方法的自定义参数")
7272

73-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
73+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
7474
container.start()
7575

7676
val executor = ContainerCommandExecutor(container)
@@ -87,7 +87,7 @@ class ContainerCommandExecutorTest {
8787
fun `测试 executeCommandWithExpectedExitCode 方法的成功情况`() {
8888
log.info("开始测试 executeCommandWithExpectedExitCode 方法的成功情况")
8989

90-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
90+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
9191
container.start()
9292

9393
val executor = ContainerCommandExecutor(container)
@@ -104,7 +104,7 @@ class ContainerCommandExecutorTest {
104104
fun `测试 executeCommandWithExpectedExitCode 方法的失败情况`() {
105105
log.info("开始测试 executeCommandWithExpectedExitCode 方法的失败情况")
106106

107-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
107+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
108108
container.start()
109109

110110
val executor = ContainerCommandExecutor(container)
@@ -119,7 +119,7 @@ class ContainerCommandExecutorTest {
119119
fun `测试 executeCommandAndGetOutput 方法`() {
120120
log.info("开始测试 executeCommandAndGetOutput 方法")
121121

122-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
122+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
123123
container.start()
124124

125125
val executor = ContainerCommandExecutor(container)
@@ -135,7 +135,7 @@ class ContainerCommandExecutorTest {
135135
fun `测试 executeCommandAndCheckOutput 方法的成功情况`() {
136136
log.info("开始测试 executeCommandAndCheckOutput 方法的成功情况")
137137

138-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
138+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
139139
container.start()
140140

141141
val executor = ContainerCommandExecutor(container)
@@ -152,7 +152,7 @@ class ContainerCommandExecutorTest {
152152
fun `测试 executeCommandAndCheckOutput 方法的失败情况`() {
153153
log.info("开始测试 executeCommandAndCheckOutput 方法的失败情况")
154154

155-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
155+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
156156
container.start()
157157

158158
val executor = ContainerCommandExecutor(container)
@@ -167,7 +167,7 @@ class ContainerCommandExecutorTest {
167167
fun `测试 waitForContainerReady 方法`() {
168168
log.info("开始测试 waitForContainerReady 方法")
169169

170-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
170+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
171171
container.start()
172172

173173
val executor = ContainerCommandExecutor(container)
@@ -185,7 +185,7 @@ class ContainerCommandExecutorTest {
185185
fun `测试 fileExists 方法的存在文件`() {
186186
log.info("开始测试 fileExists 方法的存在文件")
187187

188-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
188+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
189189
container.start()
190190

191191
val executor = ContainerCommandExecutor(container)
@@ -201,7 +201,7 @@ class ContainerCommandExecutorTest {
201201
fun `测试 fileExists 方法的不存在文件`() {
202202
log.info("开始测试 fileExists 方法的不存在文件")
203203

204-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
204+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
205205
container.start()
206206

207207
val executor = ContainerCommandExecutor(container)
@@ -217,7 +217,7 @@ class ContainerCommandExecutorTest {
217217
fun `测试 waitForFile 方法`() {
218218
log.info("开始测试 waitForFile 方法")
219219

220-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
220+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
221221
container.start()
222222

223223
val executor = ContainerCommandExecutor(container)
@@ -238,7 +238,7 @@ class ContainerCommandExecutorTest {
238238
fun `测试 readFileContent 方法`() {
239239
log.info("开始测试 readFileContent 方法")
240240

241-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
241+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
242242
container.start()
243243

244244
val executor = ContainerCommandExecutor(container)

testtoolkit/src/test/kotlin/io/github/truenine/composeserver/testtoolkit/utils/ContainerExtensionsTest.kt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ContainerExtensionsTest {
2323
fun `测试 commandExecutor 扩展函数`() {
2424
log.info("开始测试 commandExecutor 扩展函数")
2525

26-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
26+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
2727
container.start()
2828

2929
val executor = container.commandExecutor()
@@ -38,7 +38,7 @@ class ContainerExtensionsTest {
3838
fun `测试 safeExecInContainer 扩展函数`() {
3939
log.info("开始测试 safeExecInContainer 扩展函数")
4040

41-
GenericContainer(DockerImageName.parse("alpine:latest"))
41+
GenericContainer(DockerImageName.parse("alpine:3.21"))
4242
.withCommand("sleep", "300") // 让容器运行5分钟,足够测试使用
4343
.use { container ->
4444
container.start()
@@ -59,7 +59,7 @@ class ContainerExtensionsTest {
5959
fun `测试 safeExecInContainer 扩展函数带自定义参数`() {
6060
log.info("开始测试 safeExecInContainer 扩展函数带自定义参数")
6161

62-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
62+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
6363
container.start()
6464

6565
val result = container.safeExecInContainer(timeout = Duration.ofSeconds(10), maxRetries = 2, "echo", "Custom Test")
@@ -75,7 +75,7 @@ class ContainerExtensionsTest {
7575
fun `测试 execWithExpectedExitCode 扩展函数`() {
7676
log.info("开始测试 execWithExpectedExitCode 扩展函数")
7777

78-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
78+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
7979
container.start()
8080

8181
// 测试成功的命令
@@ -90,7 +90,7 @@ class ContainerExtensionsTest {
9090
fun `测试 execAndGetOutput 扩展函数`() {
9191
log.info("开始测试 execAndGetOutput 扩展函数")
9292

93-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
93+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
9494
container.start()
9595

9696
val output = container.execAndGetOutput(commands = arrayOf("echo", "Test Output"))
@@ -105,7 +105,7 @@ class ContainerExtensionsTest {
105105
fun `测试 execAndCheckOutput 扩展函数`() {
106106
log.info("开始测试 execAndCheckOutput 扩展函数")
107107

108-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
108+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
109109
container.start()
110110

111111
val result = container.execAndCheckOutput("Hello", commands = arrayOf("echo", "Hello World"))
@@ -121,7 +121,7 @@ class ContainerExtensionsTest {
121121
fun `测试 waitForReady 扩展函数`() {
122122
log.info("开始测试 waitForReady 扩展函数")
123123

124-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
124+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
125125
container.start()
126126

127127
// 容器已经启动,等待就绪应该立即完成
@@ -137,7 +137,7 @@ class ContainerExtensionsTest {
137137
fun `测试 fileExists 扩展函数`() {
138138
log.info("开始测试 fileExists 扩展函数")
139139

140-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
140+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
141141
container.start()
142142

143143
// 测试存在的文件
@@ -156,7 +156,7 @@ class ContainerExtensionsTest {
156156
fun `测试 readFile 扩展函数`() {
157157
log.info("开始测试 readFile 扩展函数")
158158

159-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
159+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
160160
container.start()
161161

162162
// 创建一个测试文件
@@ -174,7 +174,7 @@ class ContainerExtensionsTest {
174174
fun `测试 waitForFile 扩展函数`() {
175175
log.info("开始测试 waitForFile 扩展函数")
176176

177-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
177+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
178178
container.start()
179179

180180
// 创建文件
@@ -195,7 +195,7 @@ class ContainerExtensionsTest {
195195
log.info("开始测试 withStableWaitStrategy 扩展函数")
196196

197197
// 测试扩展函数的存在性,通过调用其他扩展函数来间接验证
198-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
198+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
199199
container.start()
200200

201201
// 验证容器正常启动
@@ -213,7 +213,7 @@ class ContainerExtensionsTest {
213213
fun `测试 withStableWaitStrategy 扩展函数带自定义参数`() {
214214
log.info("开始测试 withStableWaitStrategy 扩展函数带自定义参数")
215215

216-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
216+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
217217
container.start()
218218

219219
assertTrue(container.isRunning, "使用自定义参数的稳定等待策略应该正常工作")
@@ -226,7 +226,7 @@ class ContainerExtensionsTest {
226226
fun `测试 withHealthCheck 扩展函数`() {
227227
log.info("开始测试 withHealthCheck 扩展函数")
228228

229-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
229+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
230230
container.start()
231231

232232
assertTrue(container.isRunning, "使用健康检查的容器应该正常启动")
@@ -239,7 +239,7 @@ class ContainerExtensionsTest {
239239
fun `测试 withHealthCheck 扩展函数带自定义参数`() {
240240
log.info("开始测试 withHealthCheck 扩展函数带自定义参数")
241241

242-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
242+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
243243
container.start()
244244

245245
assertTrue(container.isRunning, "使用自定义健康检查参数的容器应该正常启动")
@@ -252,7 +252,7 @@ class ContainerExtensionsTest {
252252
fun `测试 startAndWaitForReady 扩展函数`() {
253253
log.info("开始测试 startAndWaitForReady 扩展函数")
254254

255-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
255+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
256256
container.startAndWaitForReady()
257257

258258
assertTrue(container.isRunning, "启动并等待就绪的容器应该处于运行状态")
@@ -265,7 +265,7 @@ class ContainerExtensionsTest {
265265
fun `测试 startAndWaitForReady 扩展函数带自定义参数`() {
266266
log.info("开始测试 startAndWaitForReady 扩展函数带自定义参数")
267267

268-
GenericContainer(DockerImageName.parse("alpine:latest")).withCommand("sleep", "infinity").use { container ->
268+
GenericContainer(DockerImageName.parse("alpine:3.21")).withCommand("sleep", "infinity").use { container ->
269269
container.startAndWaitForReady(readyTimeout = Duration.ofSeconds(20), readyPollInterval = Duration.ofMillis(200))
270270

271271
assertTrue(container.isRunning, "使用自定义参数启动并等待就绪的容器应该处于运行状态")

0 commit comments

Comments
 (0)