springboot虚拟请求
表现层测试
web环境模拟测试
虚拟请求状态匹配——执行状态的匹配
@Test
void testStatus(@Autowired MockMvc mvc) throws Exception {
// //http://localhost:8080/books
// 创建一个虚拟请求,当前访问的是books
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books1");
ResultActions actions = mvc.perform(builder);
//设定预期值,与真实值进行比较,成功测试通过,失败测试失败
// 定义本次调用的预期值
StatusResultMatchers status = MockMvcResultMatchers.status();
// 预计本次调用时成功的状态200
ResultMatcher ok = status.isOk();
// 添加与机制到本次调用过程中进行匹配
actions.andExpect(ok);
}
虚拟请求状态匹配——执行内容的匹配
@Test
void testBody(@Autowired MockMvc mvc) throws Exception {
// //http://localhost:8080/books
// 创建一个虚拟请求,当前访问的是books
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
ResultActions actions = mvc.perform(builder);
//设定预期值,与真实值进行比较,成功测试通过,失败测试失败
// 定义本次调用的预期值
ContentResultMatchers content = MockMvcResultMatchers.content();
ResultMatcher result = content.string("springboot");
// 添加与机制到本次调用过程中进行匹配
actions.andExpect(result);
}
虚拟请求状态匹配——执行内容的匹配(json)
@Test
void testJson(@Autowired MockMvc mvc) throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
ResultActions actions = mvc.perform(builder);
//设定预期值,与真实值进行比较,成功测试通过,失败测试失败
// 定义本次调用的预期值
ContentResultMatchers content = MockMvcResultMatchers.content();
ResultMatcher result = content.json("{\n" +
" \"id\": 1,\n" +
" \"name\": \"springBoot\",\n" +
" \"type\": \"springBoot\",\n" +
" \"description\": \"springBoot\"\n" +
"}");
// 添加与机制到本次调用过程中进行匹配
actions.andExpect(result);
}
这里面的东西都可以匹配
虚拟请求状态匹配——匹配响应头
@Test
void testContentType(@Autowired MockMvc mvc) throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
ResultActions actions = mvc.perform(builder);
//设定预期值,与真实值进行比较,成功测试通过,失败测试失败
// 定义本次调用的预期值
HeaderResultMatchers header = MockMvcResultMatchers.header();
ResultMatcher contentType = header.string("Content-Type","application/json");
// 添加与机制到本次调用过程中进行匹配
actions.andExpect(contentType);
}
实际应用的时候,可以把这些都放在一起
例如,把这些组合一下:
@Test
void testGetById(@Autowired MockMvc mvc) throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
ResultActions actions = mvc.perform(builder);
StatusResultMatchers status = MockMvcResultMatchers.status();
ResultMatcher ok = status.isOk();
actions.andExpect(ok);
HeaderResultMatchers header = MockMvcResultMatchers.header();
ResultMatcher contentType = header.string("Content-Type","application/json");
actions.andExpect(contentType);
ContentResultMatchers content = MockMvcResultMatchers.content();
ResultMatcher result = content.json("{\n" +
" \"id\": 1,\n" +
" \"name\": \"springBoot\",\n" +
" \"type\": \"springBoot2\",\n" +
" \"description\": \"springBoot\"\n" +
"}");
actions.andExpect(result);
}
数据层测试事务回滚
测试用例数据设定
配置一个对象来使用,比如:
package com.itheima.testcase.domain;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "testcase.book")
public class BookCase {
private int id;
private int id2;
private int type;
private String name;
private String uuid;
private long publishTime;
}