5. Mock 객체를 이용한 코드 테스트
MockHttpServletRequest req= new MockHttpServletRequest();
MockHttpServletResponse res = new
MockHttpServletResponse();
req.setParameter(“id”, “1”);
…
TestController test = new TestController();
test.handleRequest(req, res);
20. 코드 설명 (설정 부분)
• @WebAppConfiguration
• WebApplicationContext 를 생성 할 수 있도록 하는 어노테이션
• @Autowired WebApplicationContext wac;
• MockMvc객체를 생성 하기 위해 WebApplicationContext가 필요
• MockMvc mockMvc;
• mockMVC 재사용
• mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
• Test를 위한 Spring MVC 엔진을 탑제한(?) MockMVC객체를
생성
21. 예제 코드 설명(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”) // 세션 설정
• …
22. 예제 코드 설명(response)
• .andExpect(handler().handlerType(Controller.class))
• 요청에대한 맵핑된 MVC컨트롤러 클래스 확인
• .andExpect(handler().methodName("test"))
• 요청에대한 맵핑된 MVC컨트롤러의 클래스에 속한 메소드 확인
• .andDo(MockMvcResultHandlers.print());
• 해당 요청과 응답에 대한 처리를 하는 부분
• MockMvcResultHandlers.print()
• Spring Test MVC 응답 결과에 대한 모든 정보 출력 처리
23. Spring Test MVC 구조
Perform => 요청 사전조건
Expect => 응답 관련 테스트
Do => 테스트시 직접 실행
Return => 테스트 결과 반환
26. MockMvcResult
Matchers
종류
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
31. 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작성이 필요
32. …
@Before
• public void init() {
• mockMvc = MockMvcBuilders.webAppContextSetup(wac)
• .alwaysDo(MockMvcResultHandlers.print())
• // 모든 테스트 정보를 logging을 한다.
• .alwaysExpect(status().isOk())
• // 모든 테스트는 http status 코드가 200
이어야한다.
• .build();
• }
마지막 팁!
• 공통적인 테스트 및 실행할 코드를 선언하는 방법
• 하단 예제 참조
35. 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(.., ..))
• 비동기 요청에 대한 응답 정보는?
36. 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(..))
37. 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체크로 통한
에러 여부 등을 체크한다.
38. View test 주요 메소드 요약
• View name test ( 해당 요청에 선택된 View name 테스트 )
• .andExpect(view().name(..))
39. 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(..))
40. 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 객체 비교
41. 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의 수 테스트
안녕하세요 이번 발표를 하게된 KSUG의 이수홍이라고합니다.이번에는 Spring Test MVC라는 주제로 발표해 보겠습니다.일단 많은 내용이 있습니다만 짧은 시간안에 발표하기 위해 많이 단축했습니다.추가적인 기능은 문서를 확인하시면 많은 기능을 확인 할 수 있습니다.
기존에는 웹서버에서 요청 응답 테스트 할 때에도 이런 방법도 사용했었습니다.
물론 다른 방법도 있습니다.
Mock객체를 이용해서 직접 맵핑되는 Handler(보통 Controller라 불리는 )를 테스트 하기도 했습니다.
Mock을 코드테스트의 문제는 Spring MVC에서 발생하게 됩니다.
Spring MVC 3.0 이부 후터는 Request 객체를 직접 사용 추천 하지 않습니다.( 물론 가능은 합니다.)다양한 Handler를 작성할 수 있는 Spring @MVC에서는 Mock객체를 이용한 테스트는 어울리지 않습니다.
그렇습니다. 그냥 객체를 생성시켜 메소드 호출도 물론 가능합니다.
이렇게 말이죠.
문제는 과연 이 테스트의 결과가 만족스러운가요?
그 외 Spring 에서 지원하는
Spring
Spring TestMVC는 3.0 이후 사용한 @MVC 방식의 프레임워크가 기존 테스트방식으로 테스트 하기에는 많은 한계를 느끼고Spring 3.1부터 독립적이 프로젝트로 진행 해오다가 Spring3.2부터 Spring Test에 내장되었습니다.
기존 테스트 방식이 코드 기준으로 Unit 형태로 테스트를 했다고 하면
Spring Test MVC는 MockMVC라는 Spring MVC 작동하는 가상 서버 역할을 하는 객체가 만들어지게 됩니다.그 객체에서 직접 URI,Parameter, 메소드, 헤더 등으로 HttpRequest형식로 요청하게됩니다.실제 Spring MVC 프레임워크가 서버에서 작동하는 방법과 동일하게 실행시켜 실제 서버와 결과값을 동일 하게 Response형태를 받아서 테스트하게 됩니다.
실제 Web Application Server를 띄우지 않고 Spring MVC 테스트가 가능합니다.
실제 Was에서 Spring MVC를 실행한것과 Spring Test MVC에서 한것과 요청에 대한 결과는 대부분은 동일
Junit의 test를 위한 메소드는 직접 사용하지 않아도 내부적으로 사용해서 테스트를 진행합니다.
어디까지 테스트해봤니?
결과값이 Json이면 파싱을 하여 jsonPath로 테스트를 진행합니다.
권한인증 프레임워크에서는 Spring security를 많이 사용 하고 계실텐데요Spring Test MVC로 테스틑 하는 방법을 추가적으로 보여 드리겠습니다.
결과값이 Json이면 파싱을 하여 jsonPath로 테스트를 진행합니다.
해단 mockMvc를 사용하는 요청에서 공통적인 테스트 및 실행할 코드를 설정 가능한 방법이다.