一、响应状态
1.MockMvcResultMatchers
说明:模拟结果匹配。
package com.forever;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.result.StatusResultMatchers;@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启虚拟mvc调用
@AutoConfigureMockMvc
public class WebTest {@Test//形参写入或者上方写@Autowired;为方法注入资源。void testWeb(@Autowired MockMvc mockMvc) throws Exception {//发请求http://....../users;下面就是模拟的一个http请求,访问的是usersMockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users1");//执行对应的请求。ResultActions action= mockMvc.perform(builder);//设定预期的值进行比较,成功测试通过,失败测试失败。//定义本次调用的预期值StatusResultMatchers status = MockMvcResultMatchers.status();//预计本次调用时成功的:状态200ResultMatcher ok = status.isOk();//添加预期值到本次调用中进行匹配action.andExpect(ok);}}
2.匹配失败
二、响应体
1.content方法
@Test//形参写入或者上方写@Autowired;为方法注入资源。void testBody(@Autowired MockMvc mockMvc) throws Exception {//发请求http://....../users;下面就是模拟的一个http请求,访问的是usersMockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users");//执行对应的请求。ResultActions action = mockMvc.perform(builder);//设定预期的值进行比较,成功测试通过,失败测试失败。//定义本次调用的预期值ContentResultMatchers content = MockMvcResultMatchers.content();ResultMatcher resBody = content.string("springboot-good");//添加预期值到本次调用中进行匹配action.andExpect(resBody);}
2.匹配失败
三、json格式
1.实体类
package com.forever.domain;import lombok.Data;@Data
public class User {private int id;private String name;private int age;
}
2.控制层
package com.forever;import com.forever.domain.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/users")
public class UserController {@GetMappingpublic User getUser() {System.out.println("getById is running ....");User user = new User();user.setName("李四");user.setAge(18);user.setId(1);return user;}
}
3. 测试方法
说明:content.json()里面放的是json对象。
@Test//形参写入或者上方写@Autowired;为方法注入资源。void testJson(@Autowired MockMvc mockMvc) throws Exception {//发请求http://....../users;下面就是模拟的一个http请求,访问的是usersMockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users");//执行对应的请求。ResultActions action = mockMvc.perform(builder);//设定预期的值进行比较,成功测试通过,失败测试失败。//定义本次调用的预期值ContentResultMatchers content = MockMvcResultMatchers.content();ResultMatcher resBody = content.json("");//添加预期值到本次调用中进行匹配action.andExpect(resBody);}
四、content-type
1.测试方法
@Test//形参写入或者上方写@Autowired;为方法注入资源。void testContentType(@Autowired MockMvc mockMvc) throws Exception {//发请求http://....../users;下面就是模拟的一个http请求,访问的是usersMockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users");//执行对应的请求。ResultActions action = mockMvc.perform(builder);//设定预期的值进行比较,成功测试通过,失败测试失败。//定义本次调用的预期值HeaderResultMatchers header = MockMvcResultMatchers.header();ResultMatcher contentType=header.string("Content-good","application/json");//添加预期值到本次调用中进行匹配action.andExpect(contentType);}}