MockMvc 에 JSonPath 붙이기
개요
spring 의 org.springframework.test.web.servlet.MockMvc 에서 json-path 로 json reponse 처리하기
public class UserControllerTests { private MockMvc mockMvc; @Before public void setup() throws Exception { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/"); viewResolver.setSuffix(".jsp"); this.mockMvc = standaloneSetup(new UserController()).setViewResolvers(viewResolver).build(); } @Test public void submitJSonSuccess() throws Exception { String timezone = getTimezone(1941, 12, 16); this.mockMvc.perform( post("/v1/user/regist") .param("name", "Joe") .param("age", "56") .param("phone", "(347) 123-4567")) .andDo(print()) .andExpect(status().isOk()) .andExpect(redirectedUrl("aaa")) .andExpect(jsonPath("$[0].result").value("ok"); }
<dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <version>0.9.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>1.3</version> <scope>test</scope> </dependency>
Ref
- http://www.jayway.com/2012/09/08/spring-controller-tests-2-0/
- http://stackoverflow.com/questions/13362372/testing-with-spring-test-mvc-jsonpath-returns-null
- http://stackoverflow.com/questions/17711615/testing-spring-asyncresult-and-jsonpath-together