- 匹配响应执行状态
- 案例模拟
- 编写WebTest.java
- 测试运行
- 小结
- 匹配响应体(了解)
- 案例模拟
- 编写WebTest.java
- 测试运行
- 小结
- 匹配响应体(json)
- 案例模拟
- 创建User实体类
- 编辑UserController.java
- 运行测试
- 编辑WebTest.java
- 测试运行
- 小结
- 匹配响应头
- 案例模拟
- 编写WebTest.java
- 运行测试
- 小结
- 总结如何使用测试响应
更改urlTemplate为错误地址
package com.taotao;
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.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;
@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
@AutoConfigureMockMvc
public class WebTest {
@Test
void test(){
}
@Test
void testWeb(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/Users
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/Users1");
//执行对应的请求
ResultActions actions = mvc.perform(builder);
//设定预期值 与真实值进行比较,成功测试通过,失败测试失败
StatusResultMatchers status = MockMvcResultMatchers.status();
//预计本次调用时成功的:状态200
ResultMatcher ok = status.isOk();
//添加预计值到本次调用过程中进行匹配
actions.andExpect(ok);
}
}
测试运行
小结
匹配响应体(了解)
案例模拟 编写WebTest.java更改expectedContent属性为错误响应体
package com.taotao;
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.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.ContentResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
@AutoConfigureMockMvc
public class WebTest {
@Test
void test(){
}
@Test
void testBody(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/Users
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/Users");
//执行对应的请求
ResultActions actions = mvc.perform(builder);
//设定预期值 与真实值进行比较,成功测试通过,失败测试失败
//定义本次的预期值
ContentResultMatchers content = MockMvcResultMatchers.content();
//预计本次调用时成功的:状态200
ResultMatcher result = content.string("springboot1");
//添加预计值到本次调用过程中进行匹配
actions.andExpect(result);
}
}
测试运行
小结
匹配响应体(json)
案例模拟
创建User实体类
package com.taotao.domain;
import lombok.Data;
@SuppressWarnings({"all"})
@Data
public class User {
private int id;
private String name;
private int age;
private String address;
}
编辑UserController.java
package com.taotao.controller;
import com.taotao.domain.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/Users")
public class UserController {
@GetMapping
public User getById() {
System.out.println("getById is running");
User user = new User();
user.setId(1);
user.setName("涛涛");
user.setAge(18);
user.setAddress("河南");
return user;
}
}
运行测试
编辑WebTest.java运行Springboot14TestApplication.java
返回的json是上面的测试通过返回的json数据
package com.taotao;
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.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.ContentResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
@AutoConfigureMockMvc
public class WebTest {
@Test
void test(){
}
@Test
void testJosn(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/Users
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/Users");
//执行对应的请求
ResultActions actions = mvc.perform(builder);
//设定预期值 与真实值进行比较,成功测试通过,失败测试失败
//定义本次的预期值
ContentResultMatchers content = MockMvcResultMatchers.content();
//预计本次调用时成功的:状态200
ResultMatcher json = content.json("{"id":1,"name":"" +
"涛涛","age":18,"address":"河南"}");
//添加预计值到本次调用过程中进行匹配
actions.andExpect(json);
}
}
测试运行
成功
小结 匹配响应头 案例模拟 编写WebTest.java更改错误的json串,测试运行
String参数和value参数都在这
package com.taotao;
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.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.ContentResultMatchers;
import org.springframework.test.web.servlet.result.HeaderResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
@AutoConfigureMockMvc
public class WebTest {
@Test
void test(){
}
@Test
void testJosn(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/Users
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/Users");
//执行对应的请求
ResultActions actions = mvc.perform(builder);
//设定预期值 与真实值进行比较,成功测试通过,失败测试失败
//定义本次的预期值
HeaderResultMatchers header = MockMvcResultMatchers.header();
//预计本次调用时成功的:状态200
ResultMatcher string = header.string("Content-Type", "application/json");
//添加预计值到本次调用过程中进行匹配
actions.andExpect(string);
}
}
运行测试
成功
小结 总结如何使用测试响应修改数据,失败
如果时间足够,所有测试写在一起,直接全测



