|
| 1 | +package spring.memewikibe.api.controller; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import jakarta.validation.Valid; |
| 5 | +import jakarta.validation.constraints.NotBlank; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.DisplayName; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.springframework.http.MediaType; |
| 10 | +import org.springframework.test.web.servlet.MockMvc; |
| 11 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 12 | +import org.springframework.web.bind.annotation.PostMapping; |
| 13 | +import org.springframework.web.bind.annotation.RequestBody; |
| 14 | +import org.springframework.web.bind.annotation.RestController; |
| 15 | +import spring.memewikibe.support.error.ErrorType; |
| 16 | +import spring.memewikibe.support.error.MemeWikiApplicationException; |
| 17 | +import spring.memewikibe.support.response.ApiResponse; |
| 18 | + |
| 19 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 20 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 21 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 22 | + |
| 23 | +class ControllerAdviceTest { |
| 24 | + |
| 25 | + private MockMvc mockMvc; |
| 26 | + private ObjectMapper objectMapper; |
| 27 | + |
| 28 | + @BeforeEach |
| 29 | + void setUp() { |
| 30 | + objectMapper = new ObjectMapper(); |
| 31 | + mockMvc = MockMvcBuilders.standaloneSetup(new TestController()) |
| 32 | + .setControllerAdvice(new ControllerAdvice()) |
| 33 | + .build(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + @DisplayName("Validation 실패 시 필드별 에러 메시지를 반환한다") |
| 38 | + void handleValidationException() throws Exception { |
| 39 | + // given |
| 40 | + TestRequest invalidRequest = new TestRequest(""); |
| 41 | + |
| 42 | + // when & then |
| 43 | + mockMvc.perform(post("/test/validate") |
| 44 | + .contentType(MediaType.APPLICATION_JSON) |
| 45 | + .content(objectMapper.writeValueAsString(invalidRequest))) |
| 46 | + .andExpect(status().isBadRequest()) |
| 47 | + .andExpect(jsonPath("$.resultType").value("ERROR")) |
| 48 | + .andExpect(jsonPath("$.error.code").value("E400")) |
| 49 | + .andExpect(jsonPath("$.error.data.name").exists()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + @DisplayName("잘못된 JSON 형식 요청 시 적절한 에러 메시지를 반환한다") |
| 54 | + void handleHttpMessageNotReadable() throws Exception { |
| 55 | + // given |
| 56 | + String malformedJson = "{invalid json}"; |
| 57 | + |
| 58 | + // when & then |
| 59 | + mockMvc.perform(post("/test/validate") |
| 60 | + .contentType(MediaType.APPLICATION_JSON) |
| 61 | + .content(malformedJson)) |
| 62 | + .andExpect(status().isBadRequest()) |
| 63 | + .andExpect(jsonPath("$.resultType").value("ERROR")) |
| 64 | + .andExpect(jsonPath("$.error.code").value("E400")); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @DisplayName("MemeWikiApplicationException 발생 시 적절한 에러 응답을 반환한다") |
| 69 | + void handleCustomException() throws Exception { |
| 70 | + // when & then |
| 71 | + mockMvc.perform(post("/test/custom-error") |
| 72 | + .contentType(MediaType.APPLICATION_JSON)) |
| 73 | + .andExpect(status().isNotFound()) |
| 74 | + .andExpect(jsonPath("$.resultType").value("ERROR")) |
| 75 | + .andExpect(jsonPath("$.error.code").value("E404")) |
| 76 | + .andExpect(jsonPath("$.error.message").value("존재하지 않는 밈입니다.")); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @DisplayName("예상치 못한 예외 발생 시 기본 에러 응답을 반환한다") |
| 81 | + void handleUnexpectedException() throws Exception { |
| 82 | + // when & then |
| 83 | + mockMvc.perform(post("/test/unexpected-error") |
| 84 | + .contentType(MediaType.APPLICATION_JSON)) |
| 85 | + .andExpect(status().isInternalServerError()) |
| 86 | + .andExpect(jsonPath("$.resultType").value("ERROR")) |
| 87 | + .andExpect(jsonPath("$.error.code").value("E500")); |
| 88 | + } |
| 89 | + |
| 90 | + // Test controller for exception handling tests |
| 91 | + @RestController |
| 92 | + static class TestController { |
| 93 | + |
| 94 | + @PostMapping("/test/validate") |
| 95 | + public ApiResponse<String> validate(@Valid @RequestBody TestRequest request) { |
| 96 | + return ApiResponse.success("OK"); |
| 97 | + } |
| 98 | + |
| 99 | + @PostMapping("/test/custom-error") |
| 100 | + public ApiResponse<String> customError() { |
| 101 | + throw new MemeWikiApplicationException(ErrorType.MEME_NOT_FOUND); |
| 102 | + } |
| 103 | + |
| 104 | + @PostMapping("/test/unexpected-error") |
| 105 | + public ApiResponse<String> unexpectedError() { |
| 106 | + throw new RuntimeException("Unexpected error occurred"); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + record TestRequest(@NotBlank(message = "이름은 필수입니다.") String name) {} |
| 111 | +} |
0 commit comments