|
10 | 10 | import eatda.config.CorsConfig; |
11 | 11 | import eatda.config.CorsProperties; |
12 | 12 | import eatda.config.WebConfig; |
| 13 | +import jakarta.validation.Valid; |
| 14 | +import jakarta.validation.constraints.NotBlank; |
13 | 15 | import org.junit.jupiter.api.Nested; |
14 | 16 | import org.junit.jupiter.api.Test; |
15 | 17 | import org.springframework.beans.factory.annotation.Autowired; |
|
20 | 22 | import org.springframework.context.annotation.ComponentScan; |
21 | 23 | import org.springframework.context.annotation.FilterType; |
22 | 24 | import org.springframework.context.annotation.Import; |
| 25 | +import org.springframework.http.HttpMethod; |
23 | 26 | import org.springframework.http.MediaType; |
24 | 27 | import org.springframework.test.context.ActiveProfiles; |
25 | 28 | import org.springframework.test.web.servlet.MockMvc; |
| 29 | +import org.springframework.web.bind.annotation.CookieValue; |
26 | 30 | import org.springframework.web.bind.annotation.GetMapping; |
27 | 31 | import org.springframework.web.bind.annotation.PostMapping; |
28 | 32 | import org.springframework.web.bind.annotation.RequestHeader; |
29 | 33 | import org.springframework.web.bind.annotation.RequestMapping; |
30 | 34 | import org.springframework.web.bind.annotation.RequestParam; |
31 | 35 | import org.springframework.web.bind.annotation.RestController; |
| 36 | +import org.springframework.web.servlet.resource.NoResourceFoundException; |
32 | 37 |
|
33 | 38 | @WebMvcTest( |
34 | 39 | controllers = GlobalExceptionHandlerTest.TestExceptionController.class, |
@@ -78,20 +83,49 @@ public void allowedMethod() { |
78 | 83 | public void throwUnexpected() { |
79 | 84 | throw new RuntimeException("예상치 못한 예외"); |
80 | 85 | } |
| 86 | + |
| 87 | + @GetMapping("/missing-param") |
| 88 | + public void missingParam(@RequestParam String value) { |
| 89 | + } |
| 90 | + |
| 91 | + @GetMapping("/missing-cookie") |
| 92 | + public void missingCookie(@CookieValue("userId") String cookie) { |
| 93 | + } |
| 94 | + |
| 95 | + @GetMapping("/binding-exception") |
| 96 | + public void throwBindingException(@Valid TestDto dto) { |
| 97 | + } |
| 98 | + |
| 99 | + @GetMapping("/no-resource") |
| 100 | + public void throwNoResourceFound() throws NoResourceFoundException { |
| 101 | + throw new NoResourceFoundException(HttpMethod.GET, "/test/no-resource"); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public static class TestDto { |
| 106 | + @NotBlank |
| 107 | + private String field; |
| 108 | + |
| 109 | + public String getField() { |
| 110 | + return field; |
| 111 | + } |
| 112 | + |
| 113 | + public void setField(String field) { |
| 114 | + this.field = field; |
| 115 | + } |
81 | 116 | } |
82 | 117 |
|
83 | 118 | @Autowired |
84 | 119 | private MockMvc mockMvc; |
85 | 120 |
|
86 | 121 | @Nested |
87 | | - class handExceptions { |
| 122 | + class handleExceptions { |
88 | 123 |
|
89 | 124 | @Test |
90 | 125 | void 비즈니스_예외는_정의된_코드로_응답된다() throws Exception { |
91 | 126 | mockMvc.perform(get("/test/business")) |
92 | 127 | .andExpect(status().isNotFound()) |
93 | | - .andExpect(jsonPath("$.errorCode", equalTo(BusinessErrorCode.STORY_NOT_FOUND.getCode()))) |
94 | | - .andExpect(jsonPath("$.message", equalTo(BusinessErrorCode.STORY_NOT_FOUND.getMessage()))); |
| 128 | + .andExpect(jsonPath("$.errorCode", equalTo(BusinessErrorCode.STORY_NOT_FOUND.getCode()))); |
95 | 129 | } |
96 | 130 |
|
97 | 131 | @Test |
@@ -129,5 +163,33 @@ class handExceptions { |
129 | 163 | .andExpect(status().isInternalServerError()) |
130 | 164 | .andExpect(jsonPath("$.errorCode", equalTo(EtcErrorCode.INTERNAL_SERVER_ERROR.getCode()))); |
131 | 165 | } |
| 166 | + |
| 167 | + @Test |
| 168 | + void 필수_파라미터_누락은_400() throws Exception { |
| 169 | + mockMvc.perform(get("/test/missing-param")) |
| 170 | + .andExpect(status().isBadRequest()) |
| 171 | + .andExpect(jsonPath("$.errorCode", equalTo(EtcErrorCode.NO_PARAMETER_FOUND.getCode()))); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + void 필수_쿠키_누락은_400() throws Exception { |
| 176 | + mockMvc.perform(get("/test/missing-cookie")) |
| 177 | + .andExpect(status().isBadRequest()) |
| 178 | + .andExpect(jsonPath("$.errorCode", equalTo(EtcErrorCode.NO_COOKIE_FOUND.getCode()))); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + void DTO_바인딩_에러는_400() throws Exception { |
| 183 | + mockMvc.perform(get("/test/binding-exception")) |
| 184 | + .andExpect(status().isBadRequest()) |
| 185 | + .andExpect(jsonPath("$.errorCode", equalTo(EtcErrorCode.CLIENT_REQUEST_ERROR.getCode()))); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + void NoResourceFoundException_은_404() throws Exception { |
| 190 | + mockMvc.perform(get("/test/no-resource")) |
| 191 | + .andExpect(status().isNotFound()) |
| 192 | + .andExpect(jsonPath("$.errorCode", equalTo(EtcErrorCode.NO_RESOURCE_FOUND.getCode()))); |
| 193 | + } |
132 | 194 | } |
133 | 195 | } |
0 commit comments