Skip to content

Commit

Permalink
test: kotest를 활용한 단위테스트로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
hun-ca committed Jul 15, 2024
1 parent c966986 commit 1c01374
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,44 @@ package com.few.api.domain.problem.usecase
import com.few.api.domain.problem.usecase.dto.BrowseProblemsUseCaseIn
import com.few.api.repo.dao.problem.ProblemDao
import com.few.api.repo.dao.problem.record.ProblemIdsRecord
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
import io.mockk.every
import io.mockk.junit5.MockKExtension
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

import org.junit.jupiter.api.extension.ExtendWith
class BrowseProblemsUseCaseTest : BehaviorSpec({

@ExtendWith(MockKExtension::class)
class BrowseProblemsUseCaseTest {
lateinit var problemDao: ProblemDao
lateinit var useCase: BrowseProblemsUseCase
lateinit var useCaseIn: BrowseProblemsUseCaseIn

val problemDao: ProblemDao = mockk<ProblemDao>()
given("특정 아티클에 대한") {
beforeContainer {
problemDao = mockk<ProblemDao>()
useCase = BrowseProblemsUseCase(problemDao)
useCaseIn = BrowseProblemsUseCaseIn(articleId = 1L)
}

val useCase = BrowseProblemsUseCase(problemDao)
`when`("문제가 존재할 경우") {
val problemIdsRecord = ProblemIdsRecord(listOf(1, 2, 3))
every { problemDao.selectProblemsByArticleId(any()) } returns problemIdsRecord

@Test
fun `특정 아티클에 문제가 존재할 경우 문제번호가 정상적으로 조회된다`() {
// given
val useCaseIn = BrowseProblemsUseCaseIn(articleId = 1L)
val problemIdsRecord = ProblemIdsRecord(listOf(1, 2, 3))
then("문제번호가 정상적으로 조회된다") {
useCase.execute(useCaseIn)

every { problemDao.selectProblemsByArticleId(any()) } returns problemIdsRecord
verify(exactly = 1) { problemDao.selectProblemsByArticleId(any()) }
}
}

// when
useCase.execute(useCaseIn)
`when`("문제가 존재하지 않을 경우") {
every { problemDao.selectProblemsByArticleId(any()) } returns null

// then
verify(exactly = 1) { problemDao.selectProblemsByArticleId(any()) }
}

@Test
fun `특정 아티클에 문제가 존재하지 않을 경우 예외가 발생한다`() {
// given
val useCaseIn = BrowseProblemsUseCaseIn(articleId = 1L)

every { problemDao.selectProblemsByArticleId(any()) } returns null
then("예외가 발생한다") {
shouldThrow<Exception> { useCase.execute(useCaseIn) }

// when / then
Assertions.assertThrows(Exception::class.java) { useCase.execute(useCaseIn) }
verify(exactly = 1) { problemDao.selectProblemsByArticleId(any()) }
verify(exactly = 1) { problemDao.selectProblemsByArticleId(any()) }
}
}
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,72 @@ import com.few.api.domain.problem.usecase.dto.CheckProblemUseCaseIn
import com.few.api.repo.dao.problem.ProblemDao
import com.few.api.repo.dao.problem.SubmitHistoryDao
import com.few.api.repo.dao.problem.record.SelectProblemAnswerRecord
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.junit5.MockKExtension
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
class CheckProblemUseCaseTest {
class CheckProblemUseCaseTest : BehaviorSpec({

val problemDao: ProblemDao = mockk<ProblemDao>()
lateinit var problemDao: ProblemDao
lateinit var submitHistoryDao: SubmitHistoryDao
lateinit var useCase: CheckProblemUseCase

val submitHistoryDao: SubmitHistoryDao = mockk<SubmitHistoryDao>()
given("문제 정답 확인 요청이 온 상황에서") {

val useCase = CheckProblemUseCase(problemDao, submitHistoryDao)
beforeContainer {
problemDao = mockk<ProblemDao>()
submitHistoryDao = mockk<SubmitHistoryDao>()
useCase = CheckProblemUseCase(problemDao, submitHistoryDao)
}

@Test
fun `제출 값과 문제 정답이 같다`() {
// given
val submissionVal = "1"
val answer = submissionVal
val useCaseIn = CheckProblemUseCaseIn(problemId = 1L, sub = submissionVal)
val answerRecord = SelectProblemAnswerRecord(id = 1L, answer = answer, explanation = "해설입니다.")
`when`("제출 값과 문제 정답이 같을 경우") {
val submissionVal = "1"
val answer = submissionVal
val useCaseIn = CheckProblemUseCaseIn(problemId = 1L, sub = submissionVal)
val answerRecord = SelectProblemAnswerRecord(id = 1L, answer = answer, explanation = "해설입니다.")

every { problemDao.selectProblemAnswer(any()) } returns answerRecord
every { submitHistoryDao.insertSubmitHistory(any()) } returns 1L
every { problemDao.selectProblemAnswer(any()) } returns answerRecord
every { submitHistoryDao.insertSubmitHistory(any()) } returns 1L

// when
val useCaseOut = useCase.execute(useCaseIn)
then("문제가 정답처리 된다") {
val useCaseOut = useCase.execute(useCaseIn)

// then
assert(useCaseOut.isSolved)
verify(exactly = 1) { problemDao.selectProblemAnswer(any()) }
verify(exactly = 1) { submitHistoryDao.insertSubmitHistory(any()) }
}

@Test
fun `제출 값과 문제 정답이 다르다`() {
// given
val submissionVal = "1"
val answer = "2"
val useCaseIn = CheckProblemUseCaseIn(problemId = 1L, sub = submissionVal)
val answerRecord = SelectProblemAnswerRecord(id = 1L, answer = answer, explanation = "해설입니다.")
useCaseOut.isSolved shouldBe true
verify(exactly = 1) { problemDao.selectProblemAnswer(any()) }
verify(exactly = 1) { submitHistoryDao.insertSubmitHistory(any()) }
}
}

every { problemDao.selectProblemAnswer(any()) } returns answerRecord
every { submitHistoryDao.insertSubmitHistory(any()) } returns 1L
`when`("제출 값과 문제 정답이 다를 경우") {
val submissionVal = "1"
val answer = "2"
val useCaseIn = CheckProblemUseCaseIn(problemId = 1L, sub = submissionVal)
val answerRecord = SelectProblemAnswerRecord(id = 1L, answer = answer, explanation = "해설입니다.")

// when
val useCaseOut = useCase.execute(useCaseIn)
every { problemDao.selectProblemAnswer(any()) } returns answerRecord
every { submitHistoryDao.insertSubmitHistory(any()) } returns 1L

// then
assert(!useCaseOut.isSolved)
verify(exactly = 1) { problemDao.selectProblemAnswer(any()) }
verify(exactly = 1) { submitHistoryDao.insertSubmitHistory(any()) }
}
then("문제가 오답처리 된다") {
val useCaseOut = useCase.execute(useCaseIn)

@Test
fun `존재하지 않는 문제일 경우 예외가 발생한다`() {
// given
val useCaseIn = CheckProblemUseCaseIn(problemId = 1L, sub = "1")
useCaseOut.isSolved shouldBe false
verify(exactly = 1) { problemDao.selectProblemAnswer(any()) }
verify(exactly = 1) { submitHistoryDao.insertSubmitHistory(any()) }
}
}

every { problemDao.selectProblemAnswer(any()) } returns null
`when`("존재하지 않는 문제일 경우") {
val useCaseIn = CheckProblemUseCaseIn(problemId = 1L, sub = "1")

// when, then
Assertions.assertThrows(Exception::class.java) { useCase.execute(useCaseIn) }
every { problemDao.selectProblemAnswer(any()) } returns null

verify(exactly = 1) { problemDao.selectProblemAnswer(any()) }
then("예외가 발생한다") {
shouldThrow<Exception> { useCase.execute(useCaseIn) }
verify(exactly = 1) { problemDao.selectProblemAnswer(any()) }
}
}
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,55 @@ import com.few.api.repo.dao.problem.record.SelectProblemRecord
import com.few.api.repo.dao.problem.support.Content
import com.few.api.repo.dao.problem.support.Contents
import com.few.api.repo.dao.problem.support.ContentsJsonMapper
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
import io.mockk.every
import io.mockk.junit5.MockKExtension
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
class ReadProblemUseCaseTest {

val problemDao: ProblemDao = mockk<ProblemDao>()

val contentsJsonMapper: ContentsJsonMapper = mockk<ContentsJsonMapper>()

val useCase = ReadProblemUseCase(problemDao, contentsJsonMapper)

@Test
fun `문제ID로 문제를 조회힌다`() {
// given
val useCaseIn = ReadProblemUseCaseIn(problemId = 1L)
val problemRecord = SelectProblemRecord(id = 1L, title = "title", contents = "{}")
val contents = Contents(
listOf(
Content(number = 1, content = "{}"),
Content(number = 2, content = "{}")
class ReadProblemUseCaseTest : BehaviorSpec({

lateinit var problemDao: ProblemDao
lateinit var contentsJsonMapper: ContentsJsonMapper
lateinit var useCase: ReadProblemUseCase
lateinit var useCaseIn: ReadProblemUseCaseIn

given("문제를 조회할 상황에서") {
beforeContainer {
problemDao = mockk<ProblemDao>()
contentsJsonMapper = mockk<ContentsJsonMapper>()
useCase = ReadProblemUseCase(problemDao, contentsJsonMapper)
useCaseIn = ReadProblemUseCaseIn(problemId = 1L)
}

`when`("문제가 존재할 경우") {
val problemRecord = SelectProblemRecord(id = 1L, title = "title", contents = "{}")
val contents = Contents(
listOf(
Content(number = 1, content = "{}"),
Content(number = 2, content = "{}")
)
)
)

every { problemDao.selectProblemContents(any()) } returns problemRecord
every { contentsJsonMapper.toObject(any()) } returns contents

// when
useCase.execute(useCaseIn)
every { problemDao.selectProblemContents(any()) } returns problemRecord
every { contentsJsonMapper.toObject(any()) } returns contents

// then
verify(exactly = 1) { problemDao.selectProblemContents(any()) }
verify(exactly = 1) { contentsJsonMapper.toObject(any()) }
}
then("정상적으로 실행되어야 한다") {
useCase.execute(useCaseIn)

@Test
fun `문제가 존재하지 않을 경우 예외가 발생한다`() {
// given
val useCaseIn = ReadProblemUseCaseIn(problemId = 1L)
verify(exactly = 1) { problemDao.selectProblemContents(any()) }
verify(exactly = 1) { contentsJsonMapper.toObject(any()) }
}
}

every { problemDao.selectProblemContents(any()) } returns null
`when`("문제가 존재하지 않을 경우") {
every { problemDao.selectProblemContents(any()) } returns null

// when, then
Assertions.assertThrows(Exception::class.java) { useCase.execute(useCaseIn) }
then("예외가 발생해야 한다") {
shouldThrow<Exception> { useCase.execute(useCaseIn) }

verify(exactly = 1) { problemDao.selectProblemContents(any()) }
verify(exactly = 1) { problemDao.selectProblemContents(any()) }
}
}
}
}
})
Loading

0 comments on commit 1c01374

Please sign in to comment.