From ac7f9fa330bb88ad4a569c4f42211764668d90d3 Mon Sep 17 00:00:00 2001 From: pricelees Date: Wed, 23 Jul 2025 17:38:38 +0900 Subject: [PATCH] =?UTF-8?q?test:=20time=20=EB=8F=84=EB=A9=94=EC=9D=B8?= =?UTF-8?q?=EC=97=90=20=EC=B6=94=EA=B0=80=EB=90=9C=20=EC=BB=A4=EC=8A=A4?= =?UTF-8?q?=ED=85=80=20=EC=98=88=EC=99=B8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../persistence/ReservationRepositoryTest.kt | 2 +- .../time/business/TimeServiceTest.kt | 60 ++++++++++--------- .../roomescape/time/web/TimeControllerTest.kt | 25 ++++---- 3 files changed, 48 insertions(+), 39 deletions(-) diff --git a/src/test/kotlin/roomescape/reservation/infrastructure/persistence/ReservationRepositoryTest.kt b/src/test/kotlin/roomescape/reservation/infrastructure/persistence/ReservationRepositoryTest.kt index f911261b..0ae319ae 100644 --- a/src/test/kotlin/roomescape/reservation/infrastructure/persistence/ReservationRepositoryTest.kt +++ b/src/test/kotlin/roomescape/reservation/infrastructure/persistence/ReservationRepositoryTest.kt @@ -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 diff --git a/src/test/kotlin/roomescape/time/business/TimeServiceTest.kt b/src/test/kotlin/roomescape/time/business/TimeServiceTest.kt index 58d8a17a..ba12249e 100644 --- a/src/test/kotlin/roomescape/time/business/TimeServiceTest.kt +++ b/src/test/kotlin/roomescape/time/business/TimeServiceTest.kt @@ -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,63 +24,70 @@ class TimeServiceTest : FunSpec({ ) context("findTimeById") { - test("시간을 찾을 수 없으면 400 에러를 던진다.") { + test("시간을 찾을 수 없으면 예외 응답") { val id = 1L every { timeRepository.findByIdOrNull(id) } returns null - shouldThrow { + shouldThrow { 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 에러를 던진다.") { - val request = TimeCreateRequest(startAt = LocalTime.of(10, 0)) + 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 { + shouldThrow { 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 { + shouldThrow { 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 { + shouldThrow { timeService.deleteTime(id) - }.apply { - errorType shouldBe ErrorType.TIME_IS_USED_CONFLICT - httpStatus shouldBe HttpStatus.CONFLICT + }.also { + it.errorCode shouldBe TimeErrorCode.TIME_ALREADY_RESERVED } } } -}) \ No newline at end of file +}) diff --git a/src/test/kotlin/roomescape/time/web/TimeControllerTest.kt b/src/test/kotlin/roomescape/time/web/TimeControllerTest.kt index 4ce52364..19ae8465 100644 --- a/src/test/kotlin/roomescape/time/web/TimeControllerTest.kt +++ b/src/test/kotlin/roomescape/time/web/TimeControllerTest.kt @@ -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) } } } }