Skip to content

Commit 98fbf52

Browse files
committed
test: GlobalExceptionHandler 테스트 케이스 추가
1 parent d4e1e12 commit 98fbf52

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

src/test/java/eatda/exception/GlobalExceptionHandlerTest.java

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import eatda.config.CorsConfig;
1111
import eatda.config.CorsProperties;
1212
import eatda.config.WebConfig;
13+
import jakarta.validation.Valid;
14+
import jakarta.validation.constraints.NotBlank;
1315
import org.junit.jupiter.api.Nested;
1416
import org.junit.jupiter.api.Test;
1517
import org.springframework.beans.factory.annotation.Autowired;
@@ -20,15 +22,18 @@
2022
import org.springframework.context.annotation.ComponentScan;
2123
import org.springframework.context.annotation.FilterType;
2224
import org.springframework.context.annotation.Import;
25+
import org.springframework.http.HttpMethod;
2326
import org.springframework.http.MediaType;
2427
import org.springframework.test.context.ActiveProfiles;
2528
import org.springframework.test.web.servlet.MockMvc;
29+
import org.springframework.web.bind.annotation.CookieValue;
2630
import org.springframework.web.bind.annotation.GetMapping;
2731
import org.springframework.web.bind.annotation.PostMapping;
2832
import org.springframework.web.bind.annotation.RequestHeader;
2933
import org.springframework.web.bind.annotation.RequestMapping;
3034
import org.springframework.web.bind.annotation.RequestParam;
3135
import org.springframework.web.bind.annotation.RestController;
36+
import org.springframework.web.servlet.resource.NoResourceFoundException;
3237

3338
@WebMvcTest(
3439
controllers = GlobalExceptionHandlerTest.TestExceptionController.class,
@@ -78,20 +83,49 @@ public void allowedMethod() {
7883
public void throwUnexpected() {
7984
throw new RuntimeException("예상치 못한 예외");
8085
}
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+
}
81116
}
82117

83118
@Autowired
84119
private MockMvc mockMvc;
85120

86121
@Nested
87-
class handExceptions {
122+
class handleExceptions {
88123

89124
@Test
90125
void 비즈니스_예외는_정의된_코드로_응답된다() throws Exception {
91126
mockMvc.perform(get("/test/business"))
92127
.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())));
95129
}
96130

97131
@Test
@@ -129,5 +163,33 @@ class handExceptions {
129163
.andExpect(status().isInternalServerError())
130164
.andExpect(jsonPath("$.errorCode", equalTo(EtcErrorCode.INTERNAL_SERVER_ERROR.getCode())));
131165
}
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+
}
132194
}
133195
}

0 commit comments

Comments
 (0)