-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: create notice service test code(kotest + mockk)
- Loading branch information
1 parent
b411ab3
commit c1a749e
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
.../src/test/kotlin/com/knet/dormitory/domain/notice/service/KotestMockkNoticeServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.knet.dormitory.domain.notice.service | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.knet.dormitory.domain.notice.Notice | ||
import com.knet.dormitory.domain.notice.NoticeTopic | ||
import com.knet.dormitory.domain.notice.repository.NoticeRepository | ||
import io.kotest.core.spec.style.BehaviorSpec | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import org.junit.jupiter.api.Assertions | ||
import org.springframework.web.reactive.function.client.WebClient | ||
import java.time.LocalDate | ||
|
||
class KotestMockkNoticeServiceTest : BehaviorSpec({ | ||
val webClient: WebClient = mockk() | ||
val objectMapper: ObjectMapper = mockk() | ||
val noticeRepository: NoticeRepository = mockk() | ||
|
||
given("10개의 notice가 존재할 때") { | ||
val testNotices = arrayListOf<Notice>() | ||
for (i in 0 until 10) { | ||
testNotices.add( | ||
Notice( | ||
title = "title$i", | ||
writerName = "writer name$i", | ||
writerId = "writer id$i", | ||
topic = NoticeTopic.KW_DORM_COMMON, | ||
createdAt = LocalDate.now() | ||
) | ||
) | ||
} | ||
every { noticeRepository.findAllByOrderByInfoCreatedAtAsc() } returns testNotices | ||
|
||
val noticeService = DormitoryNoticeService( | ||
webClient = webClient, | ||
objectMapper = objectMapper, | ||
noticeRepository = noticeRepository | ||
) | ||
|
||
`when`("findall로 조회하면") { | ||
val notices = noticeService.findAll() | ||
|
||
then("10개의 notice가 조회되어야한다.") { | ||
Assertions.assertTrue { notices.size == 10 } | ||
} | ||
then("findAllByOrderByInfoCreatedAtAsc 가 한번만 조회되어야한다.") { | ||
verify(exactly = 1) { noticeRepository.findAllByOrderByInfoCreatedAtAsc() } | ||
} | ||
} | ||
} | ||
}) { | ||
} |