Skip to content

Commit f8a49f7

Browse files
committed
test: 테스트 추가
1 parent 98fbf52 commit f8a49f7

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package eatda.config;
2+
3+
import org.junit.jupiter.api.Nested;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.mock.web.MockHttpServletRequest;
6+
import org.springframework.mock.web.MockHttpServletResponse;
7+
8+
class LoggingInterceptorTest {
9+
10+
private final LoggingInterceptor interceptor = new LoggingInterceptor();
11+
12+
@Nested
13+
class afterCompletion {
14+
15+
@Test
16+
void preHandle_없이_afterCompletion만_호출되면_duration_unknown_로그가_남는다() {
17+
MockHttpServletRequest request = new MockHttpServletRequest();
18+
MockHttpServletResponse response = new MockHttpServletResponse();
19+
20+
interceptor.afterCompletion(request, response, new Object(), null);
21+
}
22+
}
23+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.http.MediaType;
2727
import org.springframework.test.context.ActiveProfiles;
2828
import org.springframework.test.web.servlet.MockMvc;
29+
import org.springframework.validation.annotation.Validated;
2930
import org.springframework.web.bind.annotation.CookieValue;
3031
import org.springframework.web.bind.annotation.GetMapping;
3132
import org.springframework.web.bind.annotation.PostMapping;
@@ -100,6 +101,14 @@ public void throwBindingException(@Valid TestDto dto) {
100101
public void throwNoResourceFound() throws NoResourceFoundException {
101102
throw new NoResourceFoundException(HttpMethod.GET, "/test/no-resource");
102103
}
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+
}
103112
}
104113

105114
public static class TestDto {
@@ -191,5 +200,19 @@ class handleExceptions {
191200
.andExpect(status().isNotFound())
192201
.andExpect(jsonPath("$.errorCode", equalTo(EtcErrorCode.NO_RESOURCE_FOUND.getCode())));
193202
}
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+
}
194217
}
195218
}

0 commit comments

Comments
 (0)