目录
发送虚拟请求访问controller
匹配响应执行状态
匹配响应体
匹配json格式响应体
匹配响应头
我们在test类中虚拟访问controller,就得发送虚拟请求。
先创建一个controller
package com.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/tests")
public class TestController {
@GetMapping
public String test(){
System.out.println("test is running");
return "test is success";
}
}
在test中 ,这个是一个get请求,所以我们调用get,如果是put,则调用put即可
package com;
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.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启虚拟MVC调用
@AutoConfigureMockMvc
public class WebTest {
@Test
// 注入虚拟MVC调用对象
public void test(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/tests,MockMvcRequestBuilders是一个工具类
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
//执行请求
mvc.perform(builder);
}
}
访问需要用到的一个RequestBuilder,我们按ctrl+h显示出它的实现类
运行结果
打印出了结果,说明访问成功
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启虚拟MVC调用
@AutoConfigureMockMvc
public class WebTest {
@Test
// 注入虚拟MVC调用对象
public void test(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/tests,MockMvcRequestBuilders是一个工具类
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
// 执行请求
ResultActions action = mvc.perform(builder);
//设置预期值与真实值进行比较,成功则测试通过,失败则测试不通过
//定义本次调用的预期值
StatusResultMatchers status= MockMvcResultMatchers.status();
//预计本次调用成功的状态为200
ResultMatcher ok=status.isOk();
//添加预计值到本次调用过程中进行匹配
action.andExpect(ok);
}
}
运行成功不会有任何反应
当将get改为put制造一个错误,或修改不存在的路径等其他错误,则就会报出错误信息。
虚拟请求体匹配
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启虚拟MVC调用
@AutoConfigureMockMvc
public class WebTest {
@Test
// 注入虚拟MVC调用对象
public void testBody(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/tests,MockMvcRequestBuilders是一个工具类
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
// 执行请求
ResultActions action = mvc.perform(builder);
//设置预期值与真实值进行比较,成功则测试通过,失败则测试不通过
//定义本次调用的预期值
ContentResultMatchers content = MockMvcResultMatchers.content();
//预计本次调用成功的状态为200
ResultMatcher result= content.string("test is success1");
//添加预计值到本次调用过程中进行匹配
action.andExpect(result);
}
}
如果一致则不会有任何错误信息出现, 若信息不一致,则会出现
先创建一个类pojo对象
package com.pojo;
import lombok.Data;
@Data
public class Person {
private String name;
private String age;
private String detail;
}
controller下
package com.controller;
import com.pojo.Person;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/tests")
public class TestController {
@RequestMapping("/person")
public Person testPerson(){
Person person = new Person();
person.setName("zhangsan");
person.setAge("14");
person.setDetail("xijie");
return person;
}
}
启动访问得到一组json数据
我们在测试类中修改一个,使他产生错误的信息
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启虚拟MVC调用
@AutoConfigureMockMvc
public class WebTest
@Test
// 注入虚拟MVC调用对象
public void testJson(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/tests,MockMvcRequestBuilders是一个工具类
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests/person");
// 执行请求
ResultActions action = mvc.perform(builder);
ContentResultMatchers content = MockMvcResultMatchers.content();
ResultMatcher result= content.json("{\"name\":\"zhangsan\",\"age\":\"14\",\"detail\":\"xijie1\"}");
//添加预计值到本次调用过程中进行匹配
action.andExpect(result);
}
}
运行结果
@Test
// 注入虚拟MVC调用对象
public void testHeader(@Autowired MockMvc mvc) throws Exception {
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
ResultActions action = mvc.perform(builder);
HeaderResultMatchers header = MockMvcResultMatchers.header();
ResultMatcher result = header.string("Content-Type", "application/json");
//添加预计值到本次调用过程中进行匹配
action.andExpect(result);
}
匹配了一个/tests,返回字符串的方法。,就可以看出它的差别了
@RestController
@RequestMapping("/tests")
public class TestController {
@GetMapping
public String test(){
System.out.println("test is running");
return "test is success";
}
}
一般的做法都是将这些写在同一方法。