Spring test mvc 발표자료

  • View
    4.681

  • Download
    3

Embed Size (px)

Transcript

  • 1. SPRING TEST MVC FRAMEWORK MVC Test API KSUG

2. Web Application Test? 3. 1. . 2. . 3. . 4. URI . 4. ! 5. Mock MockHttpServletRequest req= new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); req.setParameter(id, 1); TestController test = new TestController(); test.handleRequest(req, res); 6. Mock Spring MVC 7. ! Request ? @RequestMapping(value=/main", method=RequestMethod.GET) public String main(@RequestParam(id") String id) { return main; } 8. ? 9. // TestController.java @RequestMapping(value=/main", method=RequestMethod.GET) public String main(@RequestParam(id") String id) { return main; } // Test @Test public void test() throws Exception { TestController controller = new TestController(); String result = controller.main(spring83); log.debug(result : {},result); assertEqual(result, main); } // console . result : main 10. ?? 11. Interceptor ? Model ? View ? URL Controller Method ? 12. Spring MVC 13. Spring Test MVC 14. Module Test Engine call Methodcall call Class 15. Spring Test MVC Test Client Request MockMVC Response Spring MVC 16. WAS Spring MVC (request) (response) Test !! 17. Client HttpServletRequest HttpServletResponse WAS Test Mock Request Mock Response MockMVC !! 18. 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(); } 19. 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()); } } 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 => 24. () org.springframework.test.web.servlet .result.MockMvcResultMatchers 25. ? 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 27. @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 28. JSONPath ? JSONPath : http://goessner.net/articles/JsonPath/ com.jayway.jsonpath 29. ?! Servlet filter !? Spring security ? Spring Test MVC ? 30. @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 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(); } ! 33. . 34. 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 42.