test: time 도메인에 추가된 커스텀 예외 테스트 반영

This commit is contained in:
이상진 2025-07-23 17:38:38 +09:00
parent 0d33579d3f
commit ac7f9fa330
3 changed files with 48 additions and 39 deletions

View File

@ -39,7 +39,7 @@ class ReservationRepositoryTest(
}
test("입력된 시간과 일치하는 예약을 반환한다.") {
assertSoftly(reservationRepository.findByTime(time)) {
assertSoftly(reservationRepository.findAllByTime(time)) {
it shouldHaveSize 1
assertSoftly(it.first().time.startAt) { result ->
result.hour shouldBe time.startAt.hour

View File

@ -6,10 +6,9 @@ import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.mockk
import org.springframework.data.repository.findByIdOrNull
import org.springframework.http.HttpStatus
import roomescape.common.exception.ErrorType
import roomescape.common.exception.RoomescapeException
import roomescape.reservation.infrastructure.persistence.ReservationRepository
import roomescape.time.exception.TimeErrorCode
import roomescape.time.exception.TimeException
import roomescape.time.infrastructure.persistence.TimeRepository
import roomescape.time.web.TimeCreateRequest
import roomescape.util.TimeFixture
@ -25,62 +24,69 @@ class TimeServiceTest : FunSpec({
)
context("findTimeById") {
test("시간을 찾을 수 없으면 400 에러를 던진다.") {
test("시간을 찾을 수 없으면 예외 응답") {
val id = 1L
every { timeRepository.findByIdOrNull(id) } returns null
shouldThrow<RoomescapeException> {
shouldThrow<TimeException> {
timeService.findById(id)
}.apply {
errorType shouldBe ErrorType.TIME_NOT_FOUND
httpStatus shouldBe HttpStatus.BAD_REQUEST
}.also {
it.errorCode shouldBe TimeErrorCode.TIME_NOT_FOUND
}
}
}
context("addTime") {
test("중복된 시간이 있으면 409 에러를 던진다.") {
context("createTime") {
val request = TimeCreateRequest(startAt = LocalTime.of(10, 0))
test("정상 저장") {
every { timeRepository.existsByStartAt(request.startAt) } returns false
every { timeRepository.save(any()) } returns TimeFixture.create(
id = 1L,
startAt = request.startAt
)
val response = timeService.createTime(request)
response.id shouldBe 1L
}
test("중복된 시간이 있으면 예외 응답") {
every { timeRepository.existsByStartAt(request.startAt) } returns true
shouldThrow<RoomescapeException> {
shouldThrow<TimeException> {
timeService.createTime(request)
}.apply {
errorType shouldBe ErrorType.TIME_DUPLICATED
httpStatus shouldBe HttpStatus.CONFLICT
}.also {
it.errorCode shouldBe TimeErrorCode.TIME_DUPLICATED
}
}
}
context("removeTimeById") {
test("시간을 찾을 수 없으면 400 에러를 던진다.") {
test("시간을 찾을 수 없으면 예외 응답") {
val id = 1L
every { timeRepository.findByIdOrNull(id) } returns null
shouldThrow<RoomescapeException> {
shouldThrow<TimeException> {
timeService.deleteTime(id)
}.apply {
errorType shouldBe ErrorType.TIME_NOT_FOUND
httpStatus shouldBe HttpStatus.BAD_REQUEST
}.also {
it.errorCode shouldBe TimeErrorCode.TIME_NOT_FOUND
}
}
test("예약이 있는 시간이면 409 에러를 던진다.") {
test("예약이 있는 시간이면 예외 응답") {
val id = 1L
val time = TimeFixture.create()
every { timeRepository.findByIdOrNull(id) } returns time
every { reservationRepository.findByTime(time) } returns listOf(mockk())
every { reservationRepository.findAllByTime(time) } returns listOf(mockk())
shouldThrow<RoomescapeException> {
shouldThrow<TimeException> {
timeService.deleteTime(id)
}.apply {
errorType shouldBe ErrorType.TIME_IS_USED_CONFLICT
httpStatus shouldBe HttpStatus.CONFLICT
}.also {
it.errorCode shouldBe TimeErrorCode.TIME_ALREADY_RESERVED
}
}
}

View File

@ -12,9 +12,9 @@ import org.springframework.data.repository.findByIdOrNull
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import roomescape.common.config.JacksonConfig
import roomescape.common.exception.ErrorType
import roomescape.reservation.infrastructure.persistence.ReservationRepository
import roomescape.time.business.TimeService
import roomescape.time.exception.TimeErrorCode
import roomescape.time.infrastructure.persistence.TimeEntity
import roomescape.time.infrastructure.persistence.TimeRepository
import roomescape.util.ReservationFixture
@ -129,7 +129,8 @@ class TimeControllerTest(
}
}
Then("동일한 시간이 존재하면 409 응답") {
Then("동일한 시간이 존재하면 예외 응답") {
val expectedError = TimeErrorCode.TIME_DUPLICATED
every {
timeRepository.existsByStartAt(time)
} returns true
@ -139,10 +140,10 @@ class TimeControllerTest(
endpoint = endpoint,
body = request,
) {
status { isConflict() }
status { isEqualTo(expectedError.httpStatus.value()) }
content {
contentType(MediaType.APPLICATION_JSON)
jsonPath("$.errorType") { value(ErrorType.TIME_DUPLICATED.name) }
jsonPath("$.code") { value(expectedError.errorCode) }
}
}
}
@ -185,8 +186,9 @@ class TimeControllerTest(
}
}
Then("없는 시간을 조회하면 400 응답") {
Then("없는 시간을 조회하면 예외 응답") {
val id = 1L
val expectedError = TimeErrorCode.TIME_NOT_FOUND
every {
timeRepository.findByIdOrNull(id)
} returns null
@ -195,32 +197,33 @@ class TimeControllerTest(
mockMvc = mockMvc,
endpoint = "/times/$id",
) {
status { isBadRequest() }
status { isEqualTo(expectedError.httpStatus.value()) }
content {
contentType(MediaType.APPLICATION_JSON)
jsonPath("$.errorType") { value(ErrorType.TIME_NOT_FOUND.name) }
jsonPath("$.code") { value(expectedError.errorCode) }
}
}
}
Then("예약이 있는 시간을 삭제하면 409 응답") {
Then("예약이 있는 시간을 삭제하면 예외 응답") {
val id = 1L
val expectedError = TimeErrorCode.TIME_ALREADY_RESERVED
every {
timeRepository.findByIdOrNull(id)
} returns TimeFixture.create(id = id)
every {
reservationRepository.findByTime(any())
reservationRepository.findAllByTime(any())
} returns listOf(ReservationFixture.create())
runDeleteTest(
mockMvc = mockMvc,
endpoint = "/times/$id",
) {
status { isConflict() }
status { isEqualTo(expectedError.httpStatus.value()) }
content {
contentType(MediaType.APPLICATION_JSON)
jsonPath("$.errorType") { value(ErrorType.TIME_IS_USED_CONFLICT.name) }
jsonPath("$.code") { value(expectedError.errorCode) }
}
}
}