Skip to content

Commit 6577055

Browse files
committed
fix: replace runBlocking with runTest for better coroutine handling
1 parent a5e5ec7 commit 6577055

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

src/test/kotlin/com/dobby/backend/application/usecase/member/email/SendEmailCodeUseCaseTest.kt

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,16 @@ package com.dobby.backend.application.usecase.member.email
22

33
import com.dobby.backend.application.service.CoroutineDispatcherProvider
44
import com.dobby.backend.application.service.TransactionExecutor
5-
import com.dobby.backend.domain.exception.EmailAlreadyVerifiedException
65
import com.dobby.backend.domain.exception.EmailDomainNotFoundException
76
import com.dobby.backend.domain.exception.EmailNotUnivException
87
import com.dobby.backend.domain.IdGenerator
98
import com.dobby.backend.domain.gateway.email.EmailGateway
109
import com.dobby.backend.domain.gateway.email.VerificationGateway
11-
import com.dobby.backend.domain.model.Verification
12-
import com.dobby.backend.infrastructure.database.entity.enums.VerificationStatus
1310
import com.dobby.backend.util.EmailUtils
14-
import io.kotest.assertions.any
1511
import io.kotest.assertions.throwables.shouldThrow
1612
import io.kotest.core.spec.style.BehaviorSpec
17-
import io.kotest.matchers.shouldBe
1813
import io.mockk.*
19-
import kotlinx.coroutines.CoroutineScope
20-
import kotlinx.coroutines.async
21-
import kotlinx.coroutines.launch
22-
import kotlinx.coroutines.runBlocking
14+
import kotlinx.coroutines.test.runTest
2315

2416
class SendEmailCodeUseCaseTest : BehaviorSpec({
2517

@@ -35,32 +27,36 @@ class SendEmailCodeUseCaseTest : BehaviorSpec({
3527
mockkObject(EmailUtils)
3628
}
3729

38-
given("유효하지 않은 도메인의 이메일이 주어졌을 때") {
39-
val invalidEmail = "invalid@unknown.com"
40-
every { EmailUtils.isDomainExists(invalidEmail) } returns false
30+
given("유효하지 않은 도메인의 이메일이 주어졌을 때") {
31+
val invalidEmail = "invalid@unknown.com"
32+
every { EmailUtils.isDomainExists(invalidEmail) } returns false
4133

42-
`when`("이메일 인증 코드 전송을 실행하면") {
43-
then("EmailDomainNotFoundException 예외가 발생해야 한다") {
34+
`when`("이메일 인증 코드 전송을 실행하면") {
35+
then("EmailDomainNotFoundException 예외가 발생해야 한다") {
36+
runTest {
4437
shouldThrow<EmailDomainNotFoundException> {
45-
runBlocking { sendEmailCodeUseCase.execute(SendEmailCodeUseCase.Input(invalidEmail)) }
38+
sendEmailCodeUseCase.execute(SendEmailCodeUseCase.Input(invalidEmail))
4639
}
4740
}
4841
}
4942
}
43+
}
5044

51-
given("대학 이메일이 아닌 일반 이메일이 주어졌을 때") {
52-
val personalEmail = "christer10@naver.com"
53-
every { EmailUtils.isDomainExists(personalEmail) } returns true
54-
every { EmailUtils.isUnivMail(personalEmail) } returns false
45+
given("대학 이메일이 아닌 일반 이메일이 주어졌을 때") {
46+
val personalEmail = "christer10@naver.com"
47+
every { EmailUtils.isDomainExists(personalEmail) } returns true
48+
every { EmailUtils.isUnivMail(personalEmail) } returns false
5549

56-
`when`("이메일 인증 코드 전송을 실행하면") {
57-
then("EmailNotUnivException 예외가 발생해야 한다") {
50+
`when`("이메일 인증 코드 전송을 실행하면") {
51+
then("EmailNotUnivException 예외가 발생해야 한다") {
52+
runTest {
5853
shouldThrow<EmailNotUnivException> {
59-
runBlocking { sendEmailCodeUseCase.execute(SendEmailCodeUseCase.Input(personalEmail)) }
54+
sendEmailCodeUseCase.execute(SendEmailCodeUseCase.Input(personalEmail))
6055
}
6156
}
6257
}
6358
}
59+
}
6460
})
6561

6662

src/test/kotlin/com/dobby/backend/application/usecase/member/email/SendMatchingEmailUseCaseTest.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import io.kotest.matchers.shouldBe
2222
import io.kotest.assertions.throwables.shouldThrow
2323
import io.mockk.*
2424
import kotlinx.coroutines.runBlocking
25+
import kotlinx.coroutines.test.runTest
2526
import java.time.LocalDate
2627
import java.time.LocalDateTime
2728

@@ -91,11 +92,13 @@ class SendMatchingEmailUseCaseTest : BehaviorSpec({
9192

9293
coEvery { emailGateway.sendEmail(any(), any(), any()) } just Runs
9394

94-
then("이메일 전송이 성공해야 한다") {
95-
val output = runBlocking { sendMatchingEmailUseCase.execute(input) }
96-
97-
output.isSuccess shouldBe true
98-
coVerify(exactly = 1) { emailGateway.sendEmail(contactEmail, any(), any()) }
95+
then("이메일 전송이 성공해야 한다") {
96+
runTest {
97+
val output = sendMatchingEmailUseCase.execute(input)
98+
output.isSuccess shouldBe true
99+
coVerify(exactly = 1) { emailGateway.sendEmail(contactEmail, any(), any())
100+
}
101+
}
99102
}
100103
}
101104

@@ -155,11 +158,12 @@ class SendMatchingEmailUseCaseTest : BehaviorSpec({
155158
val input = SendMatchingEmailUseCase.Input(invalidEmail, experimentPosts, LocalDateTime.now())
156159

157160
then("EmailDomainNotFoundException 예외가 발생해야 한다") {
158-
val exception = shouldThrow<EmailDomainNotFoundException> {
159-
runBlocking { sendMatchingEmailUseCase.execute(input) }
161+
runTest {
162+
val exception = shouldThrow<EmailDomainNotFoundException> {
163+
runBlocking { sendMatchingEmailUseCase.execute(input) }
164+
}
165+
exception shouldBe EmailDomainNotFoundException
160166
}
161-
162-
exception shouldBe EmailDomainNotFoundException
163167
}
164168
}
165169
}

0 commit comments

Comments
 (0)