0
点赞
收藏
分享

微信扫一扫

SpringBoot Web环境模拟测试



文章目录

  • ​​1. 加载测试专用属性​​
  • ​​2. 加载测试专用配置​​
  • ​​3. web环境模拟测试​​
  • ​​4. 实际开发需求中使用​​

1. 加载测试专用属性

SpringBoot Web环境模拟测试_mvc

SpringBoot Web环境模拟测试_Web环境模拟测试_02

SpringBoot Web环境模拟测试_java_03

  • 在启动测试环境时可以通过​​properties​​参数设置测试环境专用的属性
  • 优势:比多环境开发中的测试环境影响范围更小,仅对当前测试类有效
  • ​properties​​属性可以为当前测试用例添加临时属性配置

    SpringBoot Web环境模拟测试_Web环境模拟测试_04

    SpringBoot Web环境模拟测试_spring boot_05

  • 在启动测试环境时可以通过​​args​​参数设置测试环境专用的传入参数

    SpringBoot Web环境模拟测试_spring_06

  • 当两者都存在时,可以看到加参数 args 的优先输出,这是因为在 boot 中有配置文件的高低

    SpringBoot Web环境模拟测试_Web环境模拟测试_07

    SpringBoot Web环境模拟测试_Web环境模拟测试_08

  • 加载测试临时属性应用于小范围测试环境

2. 加载测试专用配置

  • 使用​​@Import​​注解加载当前测试类专用的配置

    SpringBoot Web环境模拟测试_Web环境模拟测试_09

    SpringBoot Web环境模拟测试_mvc_10

    SpringBoot Web环境模拟测试_mvc_11

  • 加载测试范围配置应用于小范围测试环境

3. web环境模拟测试

  1. 模拟端口
    SpringBoot Web环境模拟测试_java_12
    SpringBoot Web环境模拟测试_mvc_13
    SpringBoot Web环境模拟测试_java_14
  2. 虚拟请求测试
    SpringBoot Web环境模拟测试_java_15
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启虚拟MVC调用
@AutoConfigureMockMvc
public class WebTest {
@Test
//注入虚拟MVC对象
void testWeb(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问 /books
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
//执行对应的请求
mvc.perform(builder);
}

}

启动后就可以模拟出外部请求的环境,直接访问,而不通过请求浏览器来实现了

SpringBoot Web环境模拟测试_mvc_16

  1. 虚拟请求状态匹配,验证是否成功
// 设置预期值,与真实值进行比较
@Test
void testStatus(@Autowired MockMvc mvc) throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
ResultActions action = mvc.perform(builder);
//匹配执行状态(是否预期值)
//定义执行状态匹配器,本次调用的预期值
StatusResultMatchers status = MockMvcResultMatchers.status();
//定义预期执行状态,
ResultMatcher ok = status.isOk();
//使用本次真实执行结果与预期结果进行比对
action.andExpect(ok);
}

匹配成功,返回对应请求的值

SpringBoot Web环境模拟测试_spring boot_17

失败则返回对应的错误码和信息

SpringBoot Web环境模拟测试_java_18

SpringBoot Web环境模拟测试_spring_19

SpringBoot Web环境模拟测试_spring boot_20

  1. 虚拟请求响应体匹配
    SpringBoot Web环境模拟测试_mvc_21
@Test
void testBody(@Autowired MockMvc mvc) throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
ResultActions action = mvc.perform(builder);
//匹配执行状态(是否预期值)
//定义结果匹配器
ContentResultMatchers content = MockMvcResultMatchers.content();
//定义预期执行结果
ResultMatcher result = content.string("springboot");
//使用本次真实执行结果与预期结果进行比对
action.andExpect(result);
}
  • 改成不匹配的响应体,就会报错
    SpringBoot Web环境模拟测试_mvc_22
  1. 虚拟请求响应体(JSON)匹配
@RestController
@RequestMapping
public class BookController {

@GetMapping("books")
public Book getById() {
System.out.println("getById is running...");

Book book = new Book();
book.setId(1);
book.setName("springboot");
book.setType("教育");
book.setDescription("springboot cool!");
return book;
}
}
  • 把请求到的JSON字符串直接复制到下面进行比对
    SpringBoot Web环境模拟测试_java_23
@Test
void testJson(@Autowired MockMvc mvc) throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
ResultActions action = mvc.perform(builder);
//匹配执行状态(是否预期值)
//定义结果匹配器
ContentResultMatchers content = MockMvcResultMatchers.content();
//定义预期执行结果
ResultMatcher result = content.json("{\"id\":1,\"name\":\"springboot\",\"type\":\"教育\",\"description\":\"springboot cool!\"}");
//使用本次真实执行结果与预期结果进行比对
action.andExpect(result);
}
  1. 虚拟请求响应头匹配
@Test
void testContentType(@Autowired MockMvc mvc) throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
ResultActions action = mvc.perform(builder);
//匹配执行状态(是否预期值)
//定义结果匹配器
HeaderResultMatchers header = MockMvcResultMatchers.header();
//定义预期执行结果
ResultMatcher contentType = header.string("Content-Type", "application/json");
//使用本次真实执行结果与预期结果进行比对
action.andExpect(contentType);
}
  • 如果请求头不是JSON对象,就会报错
    SpringBoot Web环境模拟测试_mvc_24
    SpringBoot Web环境模拟测试_spring_25

4. 实际开发需求中使用

  • 分别对定义的 状态、请求头、响应体等 进行 比对
@Test
void testGetById(@Autowired MockMvc mvc) throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
ResultActions action = mvc.perform(builder);

StatusResultMatchers status = MockMvcResultMatchers.status();
ResultMatcher ok = status.isOk();
action.andExpect(ok);

HeaderResultMatchers header = MockMvcResultMatchers.header();
ResultMatcher contentType = header.string("Content-Type", "application/json");
action.andExpect(contentType);


ContentResultMatchers content = MockMvcResultMatchers.content();
ResultMatcher result = content.json("{\"id\":1,\"name\":\"springboot\",\"type\":\"教育\",\"description\":\"springboot cool!\"}");
action.andExpect(result);

}
  • 一般使用 content 比较多点。


举报

相关推荐

0 条评论