spring test mvc 발표자료
TRANSCRIPT
SPRING TEST MVC FRAMEWORK
MVC 테스트 작성과 Test API 소개
KSUG 이수홍
기존의 Web Application Test?
1. 로컬 서버를 실행한다 .2. 실행할 때까지 멍하게 있는다 .3. 웹브라우저를 띄운다 .4. 웹브라우저 테스트할 URI 를 입력한다 .…무한 반복 ∞
물론 !
Mock 객체를 이용한 코드 테스트
MockHttpServletRequest req= new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
req.setParameter(“id”, “1”);
…
TestController test = new TestController();
test.handleRequest(req, res);
하지만 Mock 객체로 테스트는 Spring MVC 에서는 한계가 존재 …
앗 ! Request 객체가 어디 갔지 ?
@RequestMapping(value=”/main", method=RequestMethod.GET)
public String main(@RequestParam(”id") String id) {
…
return “main”;
}
“ 그럼 그냥 메소드 호출 해서 테스트 하면 되는것 아닌가 ?”
// TestController.java@RequestMapping(value=”/main", method=RequestMethod.GET)public String main(@RequestParam(”id") String id) { … return “main”;}
// Test@Testpublic void test() throws Exception { TestController controller = new TestController(); String result = controller.main(“spring83”); log.debug(“result : {}“,result); assertEqual(result, “main”);}
// console…. result : main
테스트는 통과…
하지만…
원하는 결과 ??
Interceptor 가 실행 후 변경된 값을 알고싶다면 ?
Model 에 어떤 값이 있는지 알고싶다면 ?어떤 View 를 선택했는지 알고싶다면 ?
현재 URL 이 어떤 Controller 의 Method를 호출하는지 알고싶다면 ?
……
Spring MVC 의 다양한 기능 테스트하기에는 “태”부족
그래서 선택한
Spring Test MVC
…
Module
기존 코드 테스트 방식
TestEngine
call
Result
Methodcall
Result
call
Result
Class
Spring Test MVC 방식
Test Client
Request
MockMVC
Response
Spring MVC
실제 WAS 에서 Spring MVC가
요청 (request) 에 응답(response) 하는 부분을 Test
에서 완벽히 재현 !!
ClientHttpServletRequest
HttpServletResponseWAS
TestMock Request
Mock ResponseMockMVC
응답과 요청이 동일 !!
예제 코드 1@RunWith(SpringJUnit4ClassRunner.class)
• @ContextConfiguration(classes=AppConfig.class)• @WebAppConfiguration• public class ControllerTest {• @Autowired• WebApplicationContext wac;• MockMvc mockMvc;• @Before• public void init() {• mockMvc =
MockMvcBuilders.webAppContextSetup(wac).build();• }
예제코드 2• @Test• public void test1() throws Exception {• mockMvc.perform(get("/test/")• .header("x-requested-with", "XMLHttpRequest")• .accept(MediaType.APPLICATION_JSON))• .andExpect(handler().handlerType(Controller.class))• .andExpect(handler().methodName("test")) • .andDo(MockMvcResultHandlers.print());• }• }
코드 설명 ( 설정 부분 )• @WebAppConfiguration
• WebApplicationContext 를 생성 할 수 있도록 하는 어노테이션• @Autowired WebApplicationContext wac;
• MockMvc 객체를 생성 하기 위해 WebApplicationContext 가 필요• MockMvc mockMvc;
• mockMVC 재사용• mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
• Test 를 위한 Spring MVC 엔진을 탑제한 (?) MockMVC 객체를 생성
예제 코드 설명 (request)• mockMvc.perform(get("/test/”)
• get => http method 설정 post, delete, put 등 사용 가능 • “/test/” => 호출 URI 를 기술 한다 .
• .accept(MediaType.APPLICATION_JSON))• accept 정보를 설정
• .header("x-requested-with", "XMLHttpRequest")• Custom Header 정보를 설정
• 참고 소스에 기술 되지 않은 요청 설정에 대한 주요 기능• .param(“key”, “value”) // 파라메터 설정• .cookie(new Cookie(“key”,”value”)) // 쿠키설정• .sessionAttr(“key”, “value”) // 세션 설정• …
예제 코드 설명 (response)• .andExpect(handler().handlerType(Controller.class))
• 요청에대한 맵핑된 MVC 컨트롤러 클래스 확인• .andExpect(handler().methodName("test"))
• 요청에대한 맵핑된 MVC 컨트롤러의 클래스에 속한 메소드 확인• .andDo(MockMvcResultHandlers.print());
• 해당 요청과 응답에 대한 처리를 하는 부분• MockMvcResultHandlers.print()
• Spring Test MVC 응답 결과에 대한 모든 정보 출력 처리
Spring Test MVC 구조
Perform => 요청 사전조건
Expect => 응답 관련 테스트
Do => 테스트시 직접 실행
Return => 테스트 결과 반환
( 참조 ) org.springframework.test.web.servlet.result.MockMvcResultMatchers
도대체 어떤 테스트를 지원하는가 ?
MockMvcResultMatchers
종류
Request Test Handler Test
Status Test
Header Test
Model Test
View Test
Content Test
ForwardUrl Test
RedirectUrl Test
Flash attribute Test
Http Servlet 관련 Test Spring MVC 관련 Test
JsonPath Test
XPath Test
Content Data Test
• @Test• public void jsonPathTest() throws Exception {• mockMvc.perform(get("/test/")• .header("x-requested-with", "XMLHttpRequest")• .accept(MediaType.APPLICATION_JSON))• .andExpect(handler().handlerType(ApiController.class))• .andExpect(handler().methodName(”getJson"))
• .andExpect(jsonPath("$.status").value("FAIL”))
• .andDo(MockMvcResultHandlers.print());• }• }
JSONPath Test 예제
JSONPath 사용 하기위해는 ?• JSONPath 의 사용방법 필요
• 참조 : http://goessner.net/articles/JsonPath/
• com.jayway.jsonpath 라이브러리 필요
추가적인 질문 ?!
Servlet filter 는 어떻게 테스트 하나요 !?Spring security 는 어떻게 테스트하나요 ?Spring Test MVC 로 가능한가요 ?…
@RunWith(SpringJUnit4ClassRunner.class)• @ContextConfiguration(classes=WebAppConfig.class)• @WebAppConfiguration• public class ControllerTest {• @Autowired • FilterChainProxy springSecurityFilterChain;• @Autowired• WebApplicationContext wac;• MockMvc mockMvc;• @Before• public void init() {• mockMvc = MockMvcBuilders.webAppContextSetup(wac)• .addFilters(springSecurityFilterChain).build();• }
Spring security 연동 예제
Spring security 연동 시 참고• 참조 소스
• 참조 : https://github.com/SpringSource/spring-test-mvc/blob/master/src/test/java/org/springframework/test/web/server/samples/context/SpringSecurityTests.java
• Spring security filter 가 설정된 MockMvc 객체를 사용시 모든 요청이 권한과 관계가 맺어지기 때문에 신경써서 Test작성이 필요
…
@Before• public void init() {• mockMvc = MockMvcBuilders.webAppContextSetup(wac)
• .alwaysDo(MockMvcResultHandlers.print())• // 모든 테스트 정보를 logging 을 한다 .• .alwaysExpect(status().isOk())• // 모든 테스트는 http status 코드가 200
이어야한다 .• .build();• }• ..
마지막 팁 !• 공통적인 테스트 및 실행할 코드를 선언하는 방법• 하단 예제 참조
감사합니다 .
부록
Request test 주요 메소드 요약• Request attribute test ( Attribute 정보 확인 테스트 )
• .andExpect(request().attribute(.., ..))
• Session attribute test ( 세션 정보 확인 테스트 )• .andExpect(request().sessionAttribute(.., ..))
• Async test ( Servlet 3.0, Spring 3.2 부터 지원 하는 비동기 요청 테스트 )• .andExpect(request(). asyncStarted(.., ..))
• 비동기요청인가 ?
• .andExpect(request(). asyncNotStarted(.., ..))• 비동기요청이 아닌가 ?
• .andExpect(request(). asyncResult(.., ..))• 비동기 요청에 대한 응답 정보는 ?
Handler test 주요 메소드 요약• Handler type test ( 요청에 맵핑 되는 Handler 타입을
테스트 )• .andExpect(handler().handlerType(..))
• Handler method name test ( 요청에 맵핑되는 Handler 메소드명 테스트 )• .andExpect(handler().methodName(..))
• Handler method type test ( 요청에 맵핑되는 Handler 메소드타입 테스트 )• .andExpect(handler().method(..))
Model test 주요 메소드 요약• Model attrbute test ( Model 속성 테스트 )
• .andExpect(model().attribute(..))• .andExpect(model().attributeExists(..))• .andExpect(model().attributeErrorCount(..))• .andExpect(model().attributeHasErrors(..))• .andExpect(model().attributeHasNoErrors(..))• .andExpect(model().attributeHasFieldErrors(..))• .andExpect(model().errorCount(..))• .andExpect(model().hasErrors(..))• .andExpect(model().hasNoErrors(..))• 일단 일일이 설명 보다는 메소드명에 모든 설명이 담겨있다 !! ( 귀찮아서 그런거
절대 아님 ! )• 대략 해당 Model 에 들어 이는 속성에 대한 값과 Validation 체크로 통한 에러 여부
등을 체크한다 .
View test 주요 메소드 요약• View name test ( 해당 요청에 선택된 View name 테스트 )
• .andExpect(view().name(..))
Status, header test 주요 메소드 요약• Status test ( 응답 상태에 대한 테스트 )
• .andExpect(status().is(..))• 응답상태 코드에 대한 테스트 ( 200: 정상 … ) • org.springframework.http HttpStatus 참조
• Error message test ( 에러메세지에 대한 테스트 )• .andExpect(status().reason(..))• 메세지 문자열 등 테스트 한다 .
• Header test ( 헤더 정보에 대한 테스트 )• .andExpect(header().string(..))• .andExpect(header().longValue(..))
Content Test 주요 메소드 요약• ContentType test( contentType 테스트 )
• .andExpect(content().contentType(..)) • .andExpect(content().contentTypeCompatibleWith(..))
• Encoding test ( content 문자열 인코딩 테스트 )• .andExpect(content().encoding(..))
• Content test ( content 테스트 )• .andExpect(content().string(..)) : content 문자열 비교• .andExpect(content().bytes(..)) : content byte 비교• .andExpect(content().xml(..)) : content xml dom 비교• .andExpect(content().node(..)) : content xml dom node 비교• .andExpect(content().source(..)) : content xml dom source 객체 비교
Other Test 주요 메소드 요약• Forwarded url test( 포워딩 URL 테스트 )
• .andExpect(forwardedUrl(..))
• Redirected url test ( 리다이렉트 URL 테스트 )• .andExpect(redirectedUrl(..))
• Flash attribute test ( Spring 3.1 부터 지원 되는 Flash attribute 값 테스트 )• .andExpect(flash().attribute(..)) : attribute 테스트• .andExpect(flash(). attributeExists(..)) : attribute 존재 여부 테스트• .andExpect(flash(). attributeCount(..)) : attribute 의 수 테스트
끝