Kotlin 테스트 패턴
TDD 방법론을 따라 Kotest와 MockK로 신뢰성 있고 유지보수 가능한 테스트를 작성하기 위한 포괄적인 Kotlin 테스트 패턴입니다.
사용 시기
- 새로운 Kotlin 함수 또는 클래스를 작성할 때
- 기존 Kotlin 코드에 테스트 커버리지를 추가할 때
- 속성 기반(Property-Based) 테스트를 구현할 때
- Kotlin 프로젝트에서 TDD 워크플로우를 따를 때
- Kover를 사용해 코드 커버리지를 구성할 때
작동 방식
- 테스트 대상 코드 식별 — 테스트할 함수, 클래스 또는 모듈을 찾습니다
- Kotest 스펙 작성 — 테스트 범위에 맞는 스펙 스타일(StringSpec, FunSpec, BehaviorSpec)을 선택합니다
- 의존성 모킹 — MockK를 사용해 테스트 대상을 격리합니다
- 테스트 실행 (RED) — 예상된 오류와 함께 테스트가 실패하는지 확인합니다
- 코드 구현 (GREEN) — 테스트를 통과하는 최소한의 코드를 작성합니다
- 리팩토링 — 테스트가 통과하는 상태를 유지하면서 구현을 개선합니다
- 커버리지 확인 —
./gradlew koverHtmlReport를 실행하고 80% 이상의 커버리지를 확인합니다
예제
다음 섹션에는 각 테스트 패턴에 대한 상세하고 실행 가능한 예제가 포함되어 있습니다:
빠른 참조
- Kotest 스펙 — Kotest 스펙 스타일의 StringSpec, FunSpec, BehaviorSpec, DescribeSpec 예제
- 모킹 — MockK의 MockK 설정, 코루틴 모킹, 인자 캡처
- TDD 워크스루 — Kotlin용 TDD 워크플로우의 EmailValidator 전체 RED/GREEN/REFACTOR 사이클
- 커버리지 — Kover 커버리지의 Kover 구성 및 명령어
- Ktor 테스트 — Ktor testApplication 테스트의 testApplication 설정
Kotlin용 TDD 워크플로우
RED-GREEN-REFACTOR 사이클
RED -> 먼저 실패하는 테스트를 작성
GREEN -> 테스트를 통과하는 최소한의 코드를 작성
REFACTOR -> 테스트가 통과하는 상태를 유지하면서 코드 개선
REPEAT -> 다음 요구사항으로 계속 진행
Kotlin에서의 단계별 TDD
// Step 1: 인터페이스/시그니처 정의
// EmailValidator.kt
package com.example.validator
fun validateEmail(email: String): Result<String> {
TODO("not implemented")
}
// Step 2: 실패하는 테스트 작성 (RED)
// EmailValidatorTest.kt
package com.example.validator
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.result.shouldBeFailure
import io.kotest.matchers.result.shouldBeSuccess
class EmailValidatorTest : StringSpec({
"valid email returns success" {
validateEmail("[email protected]").shouldBeSuccess("[email protected]")
}
"empty email returns failure" {
validateEmail("").shouldBeFailure()
}
"email without @ returns failure" {
validateEmail("userexample.com").shouldBeFailure()
}
})
// Step 3: 테스트 실행 - 실패 확인
// $ ./gradlew test
// EmailValidatorTest > valid email returns success FAILED
// kotlin.NotImplementedError: An operation is not implemented
// Step 4: 최소한의 코드 구현 (GREEN)
fun validateEmail(email: String): Result<String> {
if (email.isBlank()) return Result.failure(IllegalArgumentException("Email cannot be blank"))
if ('@' !in email) return Result.failure(IllegalArgumentException("Email must contain @"))
val regex = Regex("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$")
if (!regex.matches(email)) return Result.failure(IllegalArgumentException("Invalid email format"))
return Result.success(email)
}
// Step 5: 테스트 실행 - 통과 확인
// $ ./gradlew test
// EmailValidatorTest > valid email returns success PASSED
// EmailValidatorTest > empty email returns failure PASSED
// EmailValidatorTest > email without @ returns failure PASSED
// Step 6: 필요시 리팩토링하고 테스트가 여전히 통과하는지 확인
Kotest 스펙 스타일
StringSpec (가장 간단)
class CalculatorTest : StringSpec({
"두 양수 더하기" {
Calculator.add(2, 3) shouldBe 5
}
"음수 더하기" {
Calculator.add(-1, -2) shouldBe -3
}
"0 더하기" {
Calculator.add(0, 5) shouldBe 5
}
})
FunSpec (JUnit 스타일)
class UserServiceTest : FunSpec({
val repository = mockk<UserRepository>()
val service = UserService(repository)
test("getUser가 사용자를 찾으면 반환") {
val expected = User(id = "1", name = "Alice")
coEvery { repository.findById("1") } returns expected
val result = service.getUser("1")
result shouldBe expected
}
test("getUser가 찾지 못하면 예외 발생") {
coEvery { repository.findById("999") } returns null
shouldThrow<UserNotFoundException> {
service.getUser("999")
}
}
})
BehaviorSpec (BDD 스타일)
class OrderServiceTest : BehaviorSpec({
val repository = mockk<OrderRepository>()
val paymentService = mockk<PaymentService>()
val service = OrderService(repository, paymentService)
Given("유효한 주문 요청") {
val request = CreateOrderRequest(
userId = "user-1",
items = listOf(OrderItem("product-1", quantity = 2)),
)
When("주문이 접수되면") {
coEvery { paymentService.charge(any()) } returns PaymentResult.Success
coEvery { repository.save(any()) } answers { firstArg() }
val result = service.placeOrder(request)
Then("확인된 주문을 반환해야 함") {
result.status shouldBe OrderStatus.CONFIRMED
}
Then("결제를 청구해야 함") {
coVerify(exactly = 1) { paymentService.charge(any()) }
}
}
When("결제가 실패하면") {
coEvery { paymentService.charge(any()) } returns PaymentResult.Declined
Then("PaymentException을 발생시켜야 함") {
shouldThrow<PaymentException> {
service.placeOrder(request)
}
}
}
}
})
DescribeSpec (RSpec 스타일)
class UserValidatorTest : DescribeSpec({
describe("validateUser") {
val validator = UserValidator()
context("유효한 입력") {
it("일반 사용자를 허용") {
val user = CreateUserRequest("Alice", "[email protected]")
validator.validate(user).shouldBeValid()
}
}
context("유효하지 않은 이름") {
it("빈 이름을 거부") {
val user = CreateUserRequest("", "[email protected]")
validator.validate(user).shouldBeInvalid()
}
it("최대 길이를 초과하는 이름을 거부") {
val user = CreateUserRequest("A".repeat(256), "[email protected]")
validator.validate(user).shouldBeInvalid()
}
}
}
})
Kotest Matcher
핵심 Matcher
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.matchers.string.*
import io.kotest.matchers.collections.*
import io.kotest.matchers.nulls.*
// 동등성
result shouldBe expected
result shouldNotBe unexpected
// 문자열
name shouldStartWith "Al"
name shouldEndWith "ice"
name shouldContain "lic"
name shouldMatch Regex("[A-Z][a-z]+")
name.shouldBeBlank()
// 컬렉션
list shouldContain "item"
list shouldHaveSize 3
list.shouldBeSorted()
list.shouldContainAll("a", "b", "c")
list.shouldBeEmpty()
// Null
result.shouldNotBeNull()
result.shouldBeNull()
// 타입
result.shouldBeInstanceOf<User>()
// 숫자
count shouldBeGreaterThan 0
price shouldBeInRange 1.0..100.0
// 예외
shouldThrow<IllegalArgumentException> {
validateAge(-1)
}.message shouldBe "Age must be positive"
shouldNotThrow<Exception> {
validateAge(25)
}
커스텀 Matcher
fun beActiveUser() = object : Matcher<User> {
override fun test(value: User) = MatcherResult(
value.isActive && value.lastLogin != null,
{ "User ${value.id} should be active with a last login" },
{ "User ${value.id} should not be active" },
)
}
// 사용법
user should beActiveUser()
MockK
기본 모킹
class UserServiceTest : FunSpec({
val repository = mockk<UserRepository>()
val logger = mockk<Logger>(relaxed = true) // Relaxed: 기본값 반환
val service = UserService(repository, logger)
beforeTest {
clearMocks(repository, logger)
}
test("findUser가 repository에 위임") {
val expected = User(id = "1", name = "Alice")
every { repository.findById("1") } returns expected
val result = service.findUser("1")
result shouldBe expected
verify(exactly = 1) { repository.findById("1") }
}
test("findUser가 알 수 없는 ID에 대해 null 반환") {
every { repository.findById(any()) } returns null
val result = service.findUser("unknown")
result.shouldBeNull()
}
})
코루틴 모킹
class AsyncUserServiceTest : FunSpec({
val repository = mockk<UserRepository>()
val service = UserService(repository)
test("getUser 서스펜딩 함수") {
coEvery { repository.findById("1") } returns User(id = "1", name = "Alice")
val result = service.getUser("1")
result.name shouldBe "Alice"
coVerify { repository.findById("1") }
}
test("getUser with delay") {
coEvery { repository.findById("1") } coAnswers {
delay(100) // 비동기 작업 시뮬레이션
User(id = "1", name = "Alice")
}
val result = service.getUser("1")
result.name shouldBe "Alice"
}
})
인자 캡처
test("save가 사용자 인자를 캡처") {
val slot = slot<User>()
coEvery { repository.save(capture(slot)) } returns Unit
service.createUser(CreateUserRequest("Alice", "[email protected]"))
slot.captured.name shouldBe "Alice"
slot.captured.email shouldBe "[email protected]"
slot.captured.id.shouldNotBeNull()
}
Spy 및 부분 모킹
test("실제 객체에 대한 spy") {
val realService = UserService(repository)
val spy = spyk(realService)
every { spy.generateId() } returns "fixed-id"
spy.createUser(request)
verify { spy.generateId() } // 재정의됨
// 다른 메서드는 실제 구현 사용
}
코루틴 테스트
서스펜드 함수를 위한 runTest
import kotlinx.coroutines.test.runTest
class CoroutineServiceTest : FunSpec({
test("동시 요청이 함께 완료됨") {
runTest {
val service = DataService(testScope = this)
val result = service.fetchAllData()
result.users.shouldNotBeEmpty()
result.products.shouldNotBeEmpty()
}
}
test("지연 후 타임아웃") {
runTest {
val service = SlowService()
shouldThrow<TimeoutCancellationException> {
withTimeout(100) {
service.slowOperation() // 100ms 이상 소요
}
}
}
}
})
Flow 테스트
import io.kotest.matchers.collections.shouldContainInOrder
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runTest
class FlowServiceTest : FunSpec({
test("observeUsers가 업데이트를 방출") {
runTest {
val service = UserFlowService()
val emissions = service.observeUsers()
.take(3)
.toList()
emissions shouldHaveSize 3
emissions.last().shouldNotBeEmpty()
}
}
test("searchUsers가 입력을 디바운스") {
runTest {
val service = SearchService()
val queries = MutableSharedFlow<String>()
val results = mutableListOf<List<User>>()
val job = launch {
service.searchUsers(queries).collect { results.add(it) }
}
queries.emit("a")
queries.emit("ab")
queries.emit("abc") // 이 값만 검색을 트리거해야 함
advanceTimeBy(500)
results shouldHaveSize 1
job.cancel()
}
}
})
TestDispatcher
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
class DispatcherTest : FunSpec({
test("제어된 실행을 위한 테스트 디스패처 사용") {
val dispatcher = StandardTestDispatcher()
runTest(dispatcher) {
var completed = false
launch {
delay(1000)
completed = true
}
completed shouldBe false
advanceTimeBy(1000)
completed shouldBe true
}
}
})
속성 기반 테스트
Kotest 속성 테스트
import io.kotest.core.spec.style.FunSpec
import io.kotest.property.Arb
import io.kotest.property.arbitrary.*
import io.kotest.property.forAll
import io.kotest.property.checkAll
import kotlinx.serialization.json.Json
import kotlinx.serialization.encodeToString
import kotlinx.serialization.decodeFromString
// 참고: 아래 직렬화 라운드트립 테스트는 User 데이터 클래스가
// @Serializable (kotlinx.serialization에서 제공)로 주석 처리되어 있어야 합니다.
class PropertyTest : FunSpec({
test("문자열 뒤집기는 대합(involutory) 연산임") {
forAll<String> { s ->
s.reversed().reversed() == s
}
}
test("리스트 정렬은 멱등(idempotent) 연산임") {
forAll(Arb.list(Arb.int())) { list ->
list.sorted() == list.sorted().sorted()
}
}
test("직렬화 라운드트립이 데이터를 보존함") {
checkAll(Arb.bind(Arb.string(1..50), Arb.string(5..100)) { name, email ->
User(name = name, email = "$email@test.com")
}) { user ->
val json = Json.encodeToString(user)
val decoded = Json.decodeFromString<User>(json)
decoded shouldBe user
}
}
})
커스텀 생성기
import io.kotest.property.Arb
import io.kotest.property.arbitrary.*
// 사용자 생성기
val Arb.Companion.user: Arb<User>
get() = Arb.bind(Arb.string(1..50), Arb.email(), Arb.bool()) { name, email, active ->
User(id = UUID.randomUUID().toString(), name = name, email = email, isActive = active)
}
// 이메일 생성기
fun Arb.Companion.email(): Arb<String> = arbitrary {
val local = Arb.string(5..20).bind().lowercase()
val domain = Arb.string(3..10).bind().lowercase()
val tld = Arb.string(2..3).bind().lowercase()
"$local@$domain.$tld"
}
class CustomGeneratorTest : FunSpec({
test("커스텀 생성기로 사용자 검증") {
checkAll(Arb.user) { user ->
val validation = validateUserRegistration(user)
if (user.isActive && user.email.contains("@")) {
validation.shouldBeValid()
} else {
validation.shouldBeInvalid()
}
}
}
})
Kover 커버리지
Gradle 설정
// build.gradle.kts
plugins {
id("org.jetbrains.kotlin.jvm") version "1.9.0"
id("org.jetbrains.kotlin.plugin.serialization") version "1.9.0"
id("org.jetbrains.kotlinx.kover") version "0.7.5"
}
kotlin {
jvmToolchain(11)
}
dependencies {
testImplementation("io.kotest:kotest-runner-junit5:5.8.0")
testImplementation("io.kotest:kotest-assertions-core:5.8.0")
testImplementation("io.kotest:kotest-property:5.8.0")
testImplementation("io.mockk:mockk:1.13.10")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")
}
tasks.test {
useJUnitPlatform()
}
Kover 명령어
# HTML 리포터로 커버리지 보고서 생성
./gradlew koverHtmlReport
# 보고서 경로: build/reports/kover/html/index.html
# XML 리포터 생성 (CI 도구용)
./gradlew koverXmlReport
# 보고서 경로: build/reports/kover/xml/report.xml
# 검증을 위한 커버리지 검사
./gradlew koverVerify
# 개별 소스 세트에 대한 커버리지 검사
./gradlew koverVerify --source-set-name main
Kover 커버리지 규칙 구성
// build.gradle.kts
kover {
filters {
// 리포트에서 클래스 제외
classes {
excludes += listOf(
"com.example.dto.*",
"com.example.model.*",
"com.example.generated.*",
)
}
}
verify {
rule {
minBound(80) // 최소 라인 커버리지 80%
}
}
}