springboot虚拟请求
表现层测试
web环境模拟测试
虚拟请求状态匹配——执行状态的匹配
@Testvoid testStatus(@Autowired MockMvc mvc) throws Exception {
// //http://localhost:8080/books// 创建一个虚拟请求,当前访问的是booksMockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books1");ResultActions actions = mvc.perform(builder);//设定预期值,与真实值进行比较,成功测试通过,失败测试失败// 定义本次调用的预期值StatusResultMatchers status = MockMvcResultMatchers.status();// 预计本次调用时成功的状态200ResultMatcher ok = status.isOk();// 添加与机制到本次调用过程中进行匹配actions.andExpect(ok);}
虚拟请求状态匹配——执行内容的匹配
@Testvoid testBody(@Autowired MockMvc mvc) throws Exception {
// //http://localhost:8080/books// 创建一个虚拟请求,当前访问的是booksMockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");ResultActions actions = mvc.perform(builder);//设定预期值,与真实值进行比较,成功测试通过,失败测试失败// 定义本次调用的预期值ContentResultMatchers content = MockMvcResultMatchers.content();ResultMatcher result = content.string("springboot");// 添加与机制到本次调用过程中进行匹配actions.andExpect(result);}
虚拟请求状态匹配——执行内容的匹配(json)
@Testvoid 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);}
这里面的东西都可以匹配
虚拟请求状态匹配——匹配响应头
@Testvoid 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);}
实际应用的时候,可以把这些都放在一起
例如,把这些组合一下:
@Testvoid 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;
}