Skip to content

Commit

Permalink
test: ReadProblemUseCase 단위 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hun-ca committed Jul 14, 2024
1 parent 5d47664 commit bc919d5
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.few.api.domain.problem.usecase

import com.few.api.domain.problem.usecase.dto.ReadProblemUseCaseIn
import com.few.api.repo.dao.problem.ProblemDao
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.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 = "{}")
)
)

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

// when
useCase.execute(useCaseIn)

// then
verify(exactly = 1) { problemDao.selectProblemContents(any()) }
verify(exactly = 1) { contentsJsonMapper.toObject(any()) }
}

@Test
fun `문제가 존재하지 않을 경우 예외가 발생한다`() {
// given
val useCaseIn = ReadProblemUseCaseIn(problemId = 1L)

every { problemDao.selectProblemContents(any()) } returns null

// when, then
Assertions.assertThrows(Exception::class.java) { useCase.execute(useCaseIn) }

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

0 comments on commit bc919d5

Please sign in to comment.