一、知识点
代码中对应的知识点
1.jsonPath
1)操作符见文档
2)方法见文档
3)例子见文档
2.MockMvc(org.springframework.test.web.servlet.MockMvc)
作用是伪造一个mvc的环境
其方法使用:
- perform:执行一个RequestBuilder请求,会自动执行SpringMVC的流程并映射到相应的控制器执行处理;
- get:声明发送一个get请求的方法。MockHttpServletRequestBuilder get(String urlTemplate, Object... urlVariables):根据uri模板和uri变量值得到一个GET请求方式的。另外提供了其他的请求的方法,如:post、put、delete等。
- param:添加request的参数,如上面发送请求的时候带上了了pcode = root的参数。假如使用需要发送json数据格式的时将不能使用这种方式,可见后面被@ResponseBody注解参数的解决方法
- andExpect:添加ResultMatcher验证规则,验证控制器执行完成后结果是否正确(对返回的数据进行的判断);
- andDo:添加ResultHandler结果处理器,比如调试时打印结果到控制台(对返回的数据进行的判断);
- andReturn:最后返回相应的MvcResult;然后进行自定义验证/进行下一步的异步处理(对返回的数据进行的判断);
二、代码示例
Ⅰ实体部分
1.用户实体-User
package com.knyel.dto; public class User { private String username; private String password; public String getUsername (){ return username; } public void setUsername (String username){ this.username = username; } public String getPassword (){ return password; } public void setPassword (String password){ this.password = password; } }
Ⅱ 数据传输对象(DTO)
1.UserQueryCondition
package com.knyel.dto; public class UserQueryCondition { private String username; private int age; private int ageTo; private String phone; public String getUsername (){ return username; } public void setUsername (String username){ this.username = username; } public int getAge (){ return age; } public void setAge (int age){ this.age = age; } public int getAgeTo (){ return ageTo; } public void setAgeTo (int ageTo){ this.ageTo = ageTo; } public String getPhone (){ return phone; } public void setPhone (String phone){ this.phone = phone; } @Override public String toString (){ return "UserQueryCondition{" + "username='" + username + '\'' + ", age=" + age + ", ageTo=" + ageTo + ", phone='" + phone + '\'' + '}'; } }
Ⅲ 控制层controller
1.UserController
package com.knyel.wb.controller; import com.knyel.dto.User; import com.knyel.dto.UserQueryCondition; import org.springframework.data.web.PageableDefault; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.data.domain.Pageable; import java.util.ArrayList; import java.util.List; @RestController public class UserController { @RequestMapping(value = "/user",method = RequestMethod.GET) public List<User> query(UserQueryCondition userQueryCondition, @PageableDefault(page=1,size=10,sort="username,asc") Pageable pageable){ System.out.println(pageable.getPageNumber()); System.out.println(pageable.getSort()); System.out.println(pageable.getPageSize()); System.out.println(userQueryCondition.toString()); List<User> users=new ArrayList<>(); users.add(new User()); users.add(new User()); users.add(new User()); return users; } }
Ⅳ 单元测试类
package com.knyel.controller; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @RunWith(SpringRunner.class) @SpringBootTest public class UserControllerTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setUp (){ mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } @Test public void whenQuerySuccess () throws Exception{ mockMvc.perform(MockMvcRequestBuilders.get("/user") .param("username","knyel") .param("age","18") .param("ageTo","60") .param("phone","110") .param("size","15") .param("page","2") .param("sort","age,desc") .contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.length()").value("3"));//查询的根元素,例如$.length()代表整个传过来的json的文档 } }