Springboot MVC web 测试用例

MockMvc

spring-boot-starter-test springboot 测试框架

@RunWith(SpringRunning.class)

WebApplicationContext context;

MockMvc mockMvc = MovkMvcBuilders.webAppcontextSetup(wac).build();

movkmvc.perform() 发请求

mockMvc(MovkMvcRequestBuilders.get().contentType(MediaType.APPLICATION_JSON_UTF8))构造一个GET请求,contentType:application/json

mockMvc.perform(MockMvcRequestBuilders.get()).andExpect()请求的预期期望

mockMvc.perform(MockMvcRequestBuilders.get()).andExpect(MockMvcResultMatcher.status().isOk()) 请求的响应 是成功的

mockMvc.perform(MockMvcRequestBuilders.get()).andExpect(MockMvcResultMatcher.jsonPath("$.leng()").value(3)) 请求的返回值是一个list 长度是3 json-path github开源项目

mockMvc.perform(MockMvcRequestBuilders.get()).andReturn().getResponse().getContentAsString(); 将结果中的json 字符串转换为普通字符串

反射toString方法:ReflectionToStringBuilder.toString(Object,ToStringStyle)

你可能感兴趣的:(Spring,boot)