From 5e9d29c81714d37864441ecd1c05d6a56a304500 Mon Sep 17 00:00:00 2001 From: forge-bot Date: Tue, 14 Jul 2026 06:26:05 +0000 Subject: [PATCH] =?UTF-8?q?TA=20=EC=97=AD=ED=95=A0=20Spring=20=EA=B2=BD?= =?UTF-8?q?=EA=B3=84=20smoke=20(role-ta-live-1522-001)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ErrorResponseContractTest.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/test/java/com/runtimematrix/controller/ErrorResponseContractTest.java diff --git a/src/test/java/com/runtimematrix/controller/ErrorResponseContractTest.java b/src/test/java/com/runtimematrix/controller/ErrorResponseContractTest.java new file mode 100644 index 0000000..bd7397f --- /dev/null +++ b/src/test/java/com/runtimematrix/controller/ErrorResponseContractTest.java @@ -0,0 +1,62 @@ +package com.runtimematrix.controller; + +import com.runtimematrix.config.GlobalExceptionHandler; +import com.runtimematrix.domain.exception.BusinessException; +import com.runtimematrix.service.RoleService; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import static org.hamcrest.Matchers.*; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +/** ADR-001 RFC 7807 오류 응답 계약 테스트 */ +@WebMvcTest(RoleController.class) +@Import(GlobalExceptionHandler.class) +@DisplayName("오류 응답 계약 테스트") +class ErrorResponseContractTest { + + @Autowired private MockMvc mockMvc; + @MockBean private RoleService roleService; + + @Test + @DisplayName("역할 미존재 시 404와 RFC 7807 형식 응답 반환") + void role_not_found_returns_404_with_problem_details() throws Exception { + String roleId = "non-existent"; + when(roleService.getRole(roleId)).thenThrow(new BusinessException.RoleNotFoundException(roleId)); + + mockMvc.perform(get("/api/v1/roles/{roleId}", roleId)) + .andExpect(status().isNotFound()) + .andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON)) + .andExpect(jsonPath("$.type", startsWith("https://api.runtimematrix.com/errors/"))) + .andExpect(jsonPath("$.title", is("역할을 찾을 수 없습니다"))) + .andExpect(jsonPath("$.status", is(404))) + .andExpect(jsonPath("$.detail", containsString(roleId))) + .andExpect(jsonPath("$.instance", containsString("/api/v1/roles/"))) + .andExpect(jsonPath("$.timestamp", notNullValue())) + .andExpect(jsonPath("$.traceId", notNullValue())); + } + + @Test + @DisplayName("오류 응답에 모든 필수 필드 포함") + void error_response_contains_all_required_fields() throws Exception { + when(roleService.getRole("test")).thenThrow(new BusinessException.RoleNotFoundException("test")); + + mockMvc.perform(get("/api/v1/roles/{roleId}", "test")) + .andExpect(status().isNotFound()) + .andExpect(jsonPath("$", hasKey("type"))) + .andExpect(jsonPath("$", hasKey("title"))) + .andExpect(jsonPath("$", hasKey("status"))) + .andExpect(jsonPath("$", hasKey("detail"))) + .andExpect(jsonPath("$", hasKey("instance"))) + .andExpect(jsonPath("$", hasKey("timestamp"))) + .andExpect(jsonPath("$", hasKey("traceId"))); + } +}