From c622cb02d0f3d6edacc9976b5e9fc13eb4cb2c48 Mon Sep 17 00:00:00 2001 From: Laeng Date: Wed, 3 Jan 2024 21:18:02 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20testcontainers=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=20(#16)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 8 ++++++++ src/test/resources/application-test.yml | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index c6560a7..b421db6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -39,7 +39,10 @@ val jsoupVersion = "1.17.1" val jjwtVersion = "0.12.3" val mockkVersion = "1.13.4" val springMockkVersion = "4.0.2" +val testcontainersVersion = "1.19.3" +val testcontainersMysqlVersion = "1.19.3" val kotestVersion = "5.7.2" +val kotestExtensionTestcontainers = "2.0.2" val kotestExtensionSpring = "1.1.3" dependencies { @@ -87,9 +90,14 @@ dependencies { // Spring Mockk testImplementation("com.ninja-squad:springmockk:$springMockkVersion") + // Testcontainers + testImplementation("org.testcontainers:testcontainers:$testcontainersVersion") + testImplementation("org.testcontainers:mysql:$testcontainersMysqlVersion") + // 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") // Mock Web Server diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml index d498092..396d1f2 100644 --- a/src/test/resources/application-test.yml +++ b/src/test/resources/application-test.yml @@ -1,6 +1,7 @@ spring: datasource: - url: jdbc:h2:mem:test;MODE=MYSQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE + driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver + url: jdbc:tc:mysql:8://test_db jpa: hibernate: ddl-auto: create From 994179c5c1fd4400364b869bd67dc62279062fa1 Mon Sep 17 00:00:00 2001 From: Seokjin Jeon Date: Thu, 4 Jan 2024 08:21:21 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BB=A8=ED=85=8C=EC=9D=B4=EB=84=88=20docker-compose=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95=20=EC=B6=94=EA=B0=80=20(#73)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 2 -- .../galaxyhub/sc/GalaxyhubApplicationTests.kt | 11 +++----- .../sc/config/spec/IntegrationSpec.kt | 27 +++++++++++++++++++ src/test/resources/application-test.yml | 12 ++++++--- src/test/resources/docker-compose.yml | 21 +++++++++++++++ 5 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 src/test/kotlin/kr/galaxyhub/sc/config/spec/IntegrationSpec.kt create mode 100644 src/test/resources/docker-compose.yml diff --git a/build.gradle.kts b/build.gradle.kts index b421db6..c546bbb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -40,7 +40,6 @@ val jjwtVersion = "0.12.3" val mockkVersion = "1.13.4" val springMockkVersion = "4.0.2" val testcontainersVersion = "1.19.3" -val testcontainersMysqlVersion = "1.19.3" val kotestVersion = "5.7.2" val kotestExtensionTestcontainers = "2.0.2" val kotestExtensionSpring = "1.1.3" @@ -92,7 +91,6 @@ dependencies { // Testcontainers testImplementation("org.testcontainers:testcontainers:$testcontainersVersion") - testImplementation("org.testcontainers:mysql:$testcontainersMysqlVersion") // Kotest testImplementation("io.kotest:kotest-runner-junit5:$kotestVersion") diff --git a/src/test/kotlin/kr/galaxyhub/sc/GalaxyhubApplicationTests.kt b/src/test/kotlin/kr/galaxyhub/sc/GalaxyhubApplicationTests.kt index 75aa9d5..d9485d1 100644 --- a/src/test/kotlin/kr/galaxyhub/sc/GalaxyhubApplicationTests.kt +++ b/src/test/kotlin/kr/galaxyhub/sc/GalaxyhubApplicationTests.kt @@ -1,12 +1,9 @@ package kr.galaxyhub.sc -import org.junit.jupiter.api.Test -import org.springframework.boot.test.context.SpringBootTest +import kr.galaxyhub.sc.config.spec.IntegrationSpec -@SpringBootTest -class GalaxyhubApplicationTests { +class GalaxyhubApplicationTests : IntegrationSpec({ + describe("contextLoads") { - @Test - fun contextLoads() { } -} +}) diff --git a/src/test/kotlin/kr/galaxyhub/sc/config/spec/IntegrationSpec.kt b/src/test/kotlin/kr/galaxyhub/sc/config/spec/IntegrationSpec.kt new file mode 100644 index 0000000..7d67a27 --- /dev/null +++ b/src/test/kotlin/kr/galaxyhub/sc/config/spec/IntegrationSpec.kt @@ -0,0 +1,27 @@ +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 org.springframework.boot.test.context.SpringBootTest +import org.testcontainers.containers.output.Slf4jLogConsumer +import org.testcontainers.containers.wait.strategy.ShellStrategy + +@SpringBootTest +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() + } + } +} diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml index 396d1f2..6ea04d1 100644 --- a/src/test/resources/application-test.yml +++ b/src/test/resources/application-test.yml @@ -1,17 +1,21 @@ spring: datasource: - driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver - url: jdbc:tc:mysql:8://test_db + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:13306/test + username: test + password: 1234 jpa: hibernate: - ddl-auto: create + ddl-auto: validate properties: hibernate: format_sql: true show-sql: true open-in-view: false flyway: - enabled: false + enabled: true + baseline-on-migrate: true + baseline-version: 1 logging: level: org: diff --git a/src/test/resources/docker-compose.yml b/src/test/resources/docker-compose.yml new file mode 100644 index 0000000..e1b69a9 --- /dev/null +++ b/src/test/resources/docker-compose.yml @@ -0,0 +1,21 @@ +version: "3.3" +volumes: + mysql_data: { } +services: + test_mysql: + image: mysql:8.0.33 + ports: + - "13306:3306" + environment: + TZ: Asia/Seoul + 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"