Skip to content

Commit

Permalink
test: create notice service test code(kotest + mockk)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianea2160 committed Feb 11, 2023
1 parent b411ab3 commit c1a749e
Showing 1 changed file with 53 additions and 0 deletions.
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() }
}
}
}
}) {
}

0 comments on commit c1a749e

Please sign in to comment.