|
26 | 26 | import org.springframework.http.MediaType; |
27 | 27 | import org.springframework.test.context.ActiveProfiles; |
28 | 28 | import org.springframework.test.web.servlet.MockMvc; |
| 29 | +import org.springframework.validation.annotation.Validated; |
29 | 30 | import org.springframework.web.bind.annotation.CookieValue; |
30 | 31 | import org.springframework.web.bind.annotation.GetMapping; |
31 | 32 | import org.springframework.web.bind.annotation.PostMapping; |
@@ -100,6 +101,14 @@ public void throwBindingException(@Valid TestDto dto) { |
100 | 101 | public void throwNoResourceFound() throws NoResourceFoundException { |
101 | 102 | throw new NoResourceFoundException(HttpMethod.GET, "/test/no-resource"); |
102 | 103 | } |
| 104 | + |
| 105 | + @GetMapping("/constraint") |
| 106 | + public void throwConstraintViolation(@Validated @RequestParam @NotBlank String name) { |
| 107 | + } |
| 108 | + |
| 109 | + @GetMapping("/handler-validation") |
| 110 | + public void throwHandlerMethodValidation(@Valid TestDto dto) { |
| 111 | + } |
103 | 112 | } |
104 | 113 |
|
105 | 114 | public static class TestDto { |
@@ -191,5 +200,19 @@ class handleExceptions { |
191 | 200 | .andExpect(status().isNotFound()) |
192 | 201 | .andExpect(jsonPath("$.errorCode", equalTo(EtcErrorCode.NO_RESOURCE_FOUND.getCode()))); |
193 | 202 | } |
| 203 | + |
| 204 | + @Test |
| 205 | + void 제약조건_위반은_400() throws Exception { |
| 206 | + mockMvc.perform(get("/test/constraint?name=")) |
| 207 | + .andExpect(status().isBadRequest()) |
| 208 | + .andExpect(jsonPath("$.errorCode", equalTo(EtcErrorCode.CLIENT_REQUEST_ERROR.getCode()))); |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + void HandlerMethodValidationException은_400() throws Exception { |
| 213 | + mockMvc.perform(get("/test/handler-validation")) |
| 214 | + .andExpect(status().isBadRequest()) |
| 215 | + .andExpect(jsonPath("$.errorCode", equalTo(EtcErrorCode.VALIDATION_ERROR.getCode()))); |
| 216 | + } |
194 | 217 | } |
195 | 218 | } |
0 commit comments