Child pages
  • MockMvc 에 JSonPath 붙이기

Contents

개요

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

 

  • No labels

This page has no comments.