Skip to content

Commit

Permalink
Merge pull request #82 from KATJ-HH2/feature/lil/addTestContainer
Browse files Browse the repository at this point in the history
add: testContainer
  • Loading branch information
JangHyeonJun2 authored Oct 9, 2023
2 parents b633f50 + a8bc446 commit 140281d
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ out/
!**/src/main/**/out/
!**/src/test/**/out/
application.yml
application-test.yml
application-dev.yml
application-prod.yml

Expand Down
15 changes: 13 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ repositories {
mavenCentral()
}

extra["testcontainersVersion"] = "1.19.1"


dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
Expand All @@ -28,13 +30,22 @@ dependencies {
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.springframework.boot:spring-boot-starter-aop")

runtimeOnly("com.mysql:mysql-connector-j")

testImplementation("org.springframework.boot:spring-boot-starter-test")

// testContainer
testImplementation ("org.testcontainers:testcontainers:1.19.1") // TC 의존성
testImplementation ("org.testcontainers:junit-jupiter:1.19.1") // TC 의존성
testImplementation ("org.testcontainers:mysql:1.19.1") // MtSQL 컨테이너 사용
}

dependencyManagement {
imports {
mavenBom("org.testcontainers:testcontainers-bom:${property("testcontainersVersion")}")
}
}


tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.stereotype.Repository

@Repository
interface FavoriteRepository : JpaRepository<Favorite, Long> {
fun findByTitle(title: String): Favorite?
fun findByUserIdAndId(userId: Long?, favoriteId: Long?): Favorite?
Expand Down
13 changes: 0 additions & 13 deletions src/test/kotlin/com/hh2/katj/KatjApplicationTests.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hh2.katj.test
package com.hh2.katj

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,32 @@ import com.hh2.katj.user.model.entity.Gender
import com.hh2.katj.user.model.entity.User
import com.hh2.katj.user.model.entity.UserStatus
import com.hh2.katj.user.repository.UserRepository
import com.hh2.katj.util.annotation.KATJTestContainerE2E
import com.hh2.katj.util.exception.ExceptionMessage.DUPLICATED_DATA_ALREADY_EXISTS
import com.hh2.katj.util.exception.ExceptionMessage.ID_DOES_NOT_EXIST
import com.hh2.katj.util.exception.failWithMessage
import com.hh2.katj.util.model.BaseTestEnitity
import com.hh2.katj.util.model.RoadAddress
import org.assertj.core.api.Assertions
import org.assertj.core.api.AssertionsForInterfaceTypes.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.TestConstructor


@SpringBootTest
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
@KATJTestContainerE2E
class FavoriteServiceTest (
private val userRepository: UserRepository,
private val favoriteRepository: FavoriteRepository,
private val favoriteService: FavoriteService,
){

): BaseTestEnitity() {
@AfterEach
fun tearUp() {
userRepository.deleteAllInBatch()
favoriteRepository.deleteAllInBatch()
}


@Test
fun `사용자가 경로를 즐겨찾기에 추가한다`() {
// given
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.hh2.katj.util.annotation

import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.TestConstructor
import org.testcontainers.junit.jupiter.Testcontainers


@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@SpringBootTest
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
annotation class KATJTestContainerE2E {
}
25 changes: 25 additions & 0 deletions src/test/kotlin/com/hh2/katj/util/model/BaseTestEnitity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.hh2.katj.util.model

import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import org.testcontainers.containers.MySQLContainer
import org.testcontainers.junit.jupiter.Container

open class BaseTestEnitity {
companion object {
@Container
private val mySQLContainer = MySQLContainer<Nothing>("mysql:latest").apply {
withDatabaseName("katj")
withUsername("katj")
withPassword("katj123!")
}

@DynamicPropertySource
@JvmStatic
fun registerDynamicProperties(registry: DynamicPropertyRegistry) {
registry.add("spring.datasource.url", mySQLContainer::getJdbcUrl)
registry.add("spring.datasource.username", mySQLContainer::getUsername)
registry.add("spring.datasource.password", mySQLContainer::getPassword)
}
}
}

0 comments on commit 140281d

Please sign in to comment.