From d8cd0e04d60b4c8184095c74006b3cc5a11dafe3 Mon Sep 17 00:00:00 2001 From: Eunsol Kim <61370551+Cyma-s@users.noreply.github.com> Date: Mon, 25 Sep 2023 23:50:05 +0900 Subject: [PATCH] =?UTF-8?q?test:=20ApplicationContext=20=EC=97=90=EC=84=9C?= =?UTF-8?q?=20=EB=B9=88=EC=9D=84=20=EA=B0=80=EC=A0=B8=EC=98=A4=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tobe/AnnotationHandlerAdapterTest.java | 24 ++++++++++++------- .../tobe/AnnotationHandlerMappingTest.java | 5 +++- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapterTest.java b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapterTest.java index 0f329ad3c4..a379e4939b 100644 --- a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapterTest.java +++ b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapterTest.java @@ -4,23 +4,32 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import context.org.springframework.context.ApplicationContext; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import samples.TestController; -import webmvc.org.springframework.web.servlet.view.ModelAndView; import webmvc.org.springframework.web.servlet.mvc.handler.adapter.AnnotationHandlerAdapter; import webmvc.org.springframework.web.servlet.mvc.handler.mapping.HandlerExecution; +import webmvc.org.springframework.web.servlet.view.ModelAndView; class AnnotationHandlerAdapterTest { + private AnnotationHandlerAdapter handlerAdapter; + + @BeforeEach + void setUp() { + ApplicationContext applicationContext = new ApplicationContext("samples"); + applicationContext.initialize(); + handlerAdapter = applicationContext.getBeansOfType(AnnotationHandlerAdapter.class).get(0); + } + @DisplayName("처리할 수 있는 핸들러인지 확인한다.") @Test void supports() { // given - final var handlerAdapter = new AnnotationHandlerAdapter(); - // when // then assertThat(handlerAdapter.supports(new HandlerExecution(null, null))).isTrue(); @@ -30,8 +39,6 @@ void supports() { @Test void supportsReturnFalse() { // given - final var handlerAdapter = new AnnotationHandlerAdapter(); - // when // then assertThat(handlerAdapter.supports(new Object())).isFalse(); @@ -41,13 +48,12 @@ void supportsReturnFalse() { @Test void handle() throws Exception { // given - final var handlerAdapter = new AnnotationHandlerAdapter(); - // when final HttpServletRequest request = mock(HttpServletRequest.class); when(request.getAttribute("id")).thenReturn("1"); - final ModelAndView modelAndView = handlerAdapter.handle(request, null, new HandlerExecution(new TestController(), - TestController.class.getMethod("findUserId", HttpServletRequest.class, HttpServletResponse.class))); + final ModelAndView modelAndView = handlerAdapter.handle(request, null, + new HandlerExecution(new TestController(), + TestController.class.getMethod("findUserId", HttpServletRequest.class, HttpServletResponse.class))); // then assertThat(modelAndView.getModel().get("id")).isEqualTo("1"); diff --git a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java index c5921e2d3c..bb4c24b9ef 100644 --- a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java +++ b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java @@ -4,6 +4,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import context.org.springframework.context.ApplicationContext; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.junit.jupiter.api.BeforeEach; @@ -17,7 +18,9 @@ class AnnotationHandlerMappingTest { @BeforeEach void setUp() { - handlerMapping = new AnnotationHandlerMapping(); + ApplicationContext applicationContext = new ApplicationContext("samples"); + applicationContext.initialize(); + handlerMapping = applicationContext.getBeansOfType(AnnotationHandlerMapping.class).get(0); handlerMapping.initialize(); }