Skip to content

Commit

Permalink
test: 테스트 케이스 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
BGuga committed Sep 13, 2023
1 parent 9bc9b97 commit c852c27
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
Expand All @@ -21,7 +20,6 @@
import java.util.Map;
import web.org.springframework.web.bind.annotation.RequestMapping;
import web.org.springframework.web.bind.annotation.RequestMethod;
import webmvc.org.springframework.web.servlet.ModelAndView;

public class AnnotationHandlerMapping {

Expand Down
28 changes: 28 additions & 0 deletions mvc/src/test/java/duplicate/case3/TestController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package duplicate.case3;

import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import web.org.springframework.web.bind.annotation.RequestMapping;
import web.org.springframework.web.bind.annotation.RequestMethod;
import webmvc.org.springframework.web.servlet.ModelAndView;

@Controller
public class TestController {

private static final Logger log = LoggerFactory.getLogger(samples.TestController.class);

@RequestMapping(value = "/get-test", method = {
RequestMethod.GET,
RequestMethod.GET,
RequestMethod.GET,
RequestMethod.GET,
RequestMethod.GET
})
public ModelAndView duplicatedMethod1(final HttpServletRequest request,
final HttpServletResponse response) {
return null;
}
}
17 changes: 17 additions & 0 deletions mvc/src/test/java/samples/TestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,21 @@ public ModelAndView save(final HttpServletRequest request, final HttpServletResp
modelAndView.addObject("id", request.getAttribute("id"));
return modelAndView;
}

@RequestMapping(value = "/multi-method-test", method = {RequestMethod.GET, RequestMethod.POST} )
public ModelAndView multiHandle(final HttpServletRequest request, final HttpServletResponse response) {
log.info("test controller multi-handle method");
String method = request.getMethod();
if("GET".equals(method)){
final var modelAndView = new ModelAndView(new JspView(""));
modelAndView.addObject("id", "getPooh");
return modelAndView;
}
if("POST".equals(method)){
final var modelAndView = new ModelAndView(new JspView(""));
modelAndView.addObject("id", "postPooh");
return modelAndView;
}
throw new IllegalArgumentException("해당 요청을 Handling 할 수 없는 핸들러입니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void setUp() {
}

@Test
void methodDuplicationException_case1() {
void methodDuplicationException_existMappingInOtherClass_case1() {
// given & when & then
AnnotationHandlerMapping duplicatedHandlerMapping = new AnnotationHandlerMapping(
"samples",
Expand All @@ -32,7 +32,7 @@ void methodDuplicationException_case1() {
}

@Test
void methodDuplicationException_case2() {
void methodDuplicationException_existMappingIntSameClass_case2() {
// given & when & then
AnnotationHandlerMapping duplicatedHandlerMapping = new AnnotationHandlerMapping(
"duplicate.case2");
Expand All @@ -41,6 +41,36 @@ void methodDuplicationException_case2() {
.hasMessage("Duplicated HandlerKey");
}

@Test
void methodDuplicationException_duplicateMappingInOneMethod_case3() {
// given & when & then
AnnotationHandlerMapping duplicatedHandlerMapping = new AnnotationHandlerMapping(
"duplicate.case3");
assertThatThrownBy(() -> duplicatedHandlerMapping.initialize())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Duplicated HandlerKey");
}

@Test
void oneMethodCanCreateManyHttpRequestMappings() throws Exception {
final var request = mock(HttpServletRequest.class);
final var response = mock(HttpServletResponse.class);

when(request.getAttribute("id")).thenReturn("gugu");
when(request.getRequestURI()).thenReturn("/multi-method-test");
when(request.getMethod()).thenReturn("GET");

final var handlerExecution = (HandlerExecution) handlerMapping.getHandler(request);
final var getModelAndView = handlerExecution.handle(request, response);

when(request.getMethod()).thenReturn("POST");

final var postModelAndView = handlerExecution.handle(request, response);

assertThat(getModelAndView.getObject("id")).isEqualTo("getPooh");
assertThat(postModelAndView.getObject("id")).isEqualTo("postPooh");
}

@Test
void get() throws Exception {
final var request = mock(HttpServletRequest.class);
Expand Down

0 comments on commit c852c27

Please sign in to comment.