Skip to content

Commit

Permalink
feat: testcontainers 추가 (#16) (#72)
Browse files Browse the repository at this point in the history
Co-authored-by: Seokjin Jeon <[email protected]>
  • Loading branch information
Laeng and seokjin8678 authored Jan 4, 2024
1 parent 4970c54 commit 1e64e43
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
6 changes: 6 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ 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 kotestVersion = "5.7.2"
val kotestExtensionTestcontainers = "2.0.2"
val kotestExtensionSpring = "1.1.3"

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

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

// 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
Expand Down
11 changes: 4 additions & 7 deletions src/test/kotlin/kr/galaxyhub/sc/GalaxyhubApplicationTests.kt
Original file line number Diff line number Diff line change
@@ -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() {
}
}
})
27 changes: 27 additions & 0 deletions src/test/kotlin/kr/galaxyhub/sc/config/spec/IntegrationSpec.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
11 changes: 8 additions & 3 deletions src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
spring:
datasource:
url: jdbc:h2:mem:test;MODE=MYSQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
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:
Expand Down
21 changes: 21 additions & 0 deletions src/test/resources/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 1e64e43

Please sign in to comment.