Skip to content

Commit

Permalink
test: ApplicationContext 에서 빈을 가져오도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyma-s committed Sep 25, 2023
1 parent 6e34678 commit d8cd0e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -30,8 +39,6 @@ void supports() {
@Test
void supportsReturnFalse() {
// given
final var handlerAdapter = new AnnotationHandlerAdapter();

// when
// then
assertThat(handlerAdapter.supports(new Object())).isFalse();
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

Expand Down

0 comments on commit d8cd0e0

Please sign in to comment.