Skip to content

Commit

Permalink
fix: Testcontainers 사용 시 버그 수정 (#82) (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
seokjin8678 authored Jan 9, 2024
1 parent 579318f commit 1b00e0f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 43 deletions.
8 changes: 3 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ val mockkVersion = "1.13.4"
val springMockkVersion = "4.0.2"
val testcontainersVersion = "1.19.3"
val kotestVersion = "5.7.2"
val kotestExtensionTestcontainers = "2.0.2"
val kotestExtensionSpring = "1.1.3"
val kotestExtensionSpringVersion = "1.1.3"

dependencies {
// Spring
Expand Down Expand Up @@ -90,13 +89,12 @@ dependencies {
testImplementation("com.ninja-squad:springmockk:$springMockkVersion")

// Testcontainers
testImplementation("org.testcontainers:testcontainers:$testcontainersVersion")
testImplementation("org.testcontainers:testcontainers")

// Kotest
testImplementation("io.kotest:kotest-runner-junit5:$kotestVersion")
testImplementation("io.kotest:kotest-assertions-core:$kotestVersion")
testImplementation("io.kotest.extensions:kotest-extensions-testcontainers:$kotestExtensionTestcontainers")
testImplementation("io.kotest.extensions:kotest-extensions-spring:$kotestExtensionSpring")
testImplementation("io.kotest.extensions:kotest-extensions-spring:$kotestExtensionSpringVersion")

// Mock Web Server
testImplementation("com.squareup.okhttp3:mockwebserver")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package kr.galaxyhub.sc.config

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class EnableTestcontianers
9 changes: 0 additions & 9 deletions src/test/kotlin/kr/galaxyhub/sc/config/KotestProjectConfig.kt

This file was deleted.

43 changes: 43 additions & 0 deletions src/test/kotlin/kr/galaxyhub/sc/config/TestContainerExtension.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package kr.galaxyhub.sc.config

import io.kotest.core.extensions.ProjectExtension
import io.kotest.core.project.ProjectContext
import io.kotest.core.spec.AutoScan
import io.kotest.mpp.hasAnnotation
import java.io.File
import kotlin.reflect.full.superclasses
import org.slf4j.LoggerFactory
import org.testcontainers.containers.DockerComposeContainer
import org.testcontainers.containers.output.Slf4jLogConsumer
import org.testcontainers.containers.wait.strategy.ShellStrategy

@AutoScan
class TestContainerExtension : ProjectExtension {

private val containers = mutableListOf<DockerComposeContainer<*>>()

override suspend fun interceptProject(context: ProjectContext, callback: suspend (ProjectContext) -> Unit) {
if (isTestcontianersEnable(context)) {
DockerComposeContainer(File("src/test/resources/docker-compose.yml")).apply {
waitingFor(
"test_mysql",
ShellStrategy().withCommand("mysql -u'test' -p'1234' -e'select 1' && sleep 2")
)
withLogConsumer("test_mysql", Slf4jLogConsumer(LoggerFactory.getLogger(javaClass)))
start()
}.also {
containers.add(it)
}
}
callback(context)
containers.forEach { it.stop() }
}

private fun isTestcontianersEnable(context: ProjectContext): Boolean {
val hasAnnotationItself = context.suite.specs.stream()
.anyMatch { it.kclass.hasAnnotation<EnableTestcontianers>() }
return if (hasAnnotationItself) true else context.suite.specs.stream()
.flatMap { it.kclass.superclasses.stream() }
.anyMatch { it.hasAnnotation<EnableTestcontianers>() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kr.galaxyhub.sc.config.spec
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.core.test.TestCase
import io.kotest.core.test.TestResult
import io.kotest.extensions.spring.SpringExtension
import io.mockk.clearAllMocks
import io.mockk.mockkClass
import org.junit.platform.commons.util.ClassFilter
Expand All @@ -19,6 +20,8 @@ import org.springframework.stereotype.Service
@AutoConfigureRestDocs
abstract class ControllerTestSpec(body: DescribeSpec.() -> Unit = {}) : DescribeSpec(body) {

override fun extensions() = listOf(SpringExtension)

override suspend fun afterEach(testCase: TestCase, result: TestResult) {
clearAllMocks()
}
Expand Down
22 changes: 4 additions & 18 deletions src/test/kotlin/kr/galaxyhub/sc/config/spec/IntegrationSpec.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
package kr.galaxyhub.sc.config.spec

import io.kotest.core.extensions.install
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.extensions.testcontainers.ContainerLifecycleMode
import io.kotest.extensions.testcontainers.DockerComposeContainerExtension
import java.io.File
import org.slf4j.LoggerFactory
import io.kotest.extensions.spring.SpringExtension
import kr.galaxyhub.sc.config.EnableTestcontianers
import org.springframework.boot.test.context.SpringBootTest
import org.testcontainers.containers.output.Slf4jLogConsumer
import org.testcontainers.containers.wait.strategy.ShellStrategy

@SpringBootTest
@EnableTestcontianers
abstract class IntegrationSpec(body: DescribeSpec.() -> Unit = {}) : DescribeSpec(body) {

private val log = LoggerFactory.getLogger(javaClass)

init {
install(DockerComposeContainerExtension(File("src/test/resources/docker-compose.yml"), ContainerLifecycleMode.Project))
.apply {
waitingFor("test_mysql", ShellStrategy().withCommand("mysql -u'test' -p'1234' -e'select 1' && sleep 2"))
withLogConsumer("test_mysql", Slf4jLogConsumer(log))
}.apply {
start()
}
}
override fun extensions() = listOf(SpringExtension)
}
16 changes: 5 additions & 11 deletions src/test/resources/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
version: "3.3"
volumes:
mysql_data: { }
version: "3"
services:
test_mysql:
image: mysql:8.0.33
ports:
- "13306:3306"
environment:
TZ: Asia/Seoul
MYSQL_DATABASE: test
MYSQL_ROOT_PASSWORD: 1234
MYSQL_USER: test
MYSQL_PASSWORD: 1234
MYSQL_ROOT_PASSWORD: 1234
MYSQL_DATABASE: test
volumes:
- mysql_data:/var/lib/mysql/
- ./sql:/docker-entrypoint-initdb.d
command:
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
- "--skip-character-set-client-handshake"
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci

0 comments on commit 1b00e0f

Please sign in to comment.