虚拟请求匹配响应体
还是那个Controller,这次我们需要对响应请求体进行匹配,也就是这个返回值。

设定预期值 与真实值进行比较,成功测试通过,失败则测试失败
@Test
void testBody(@Autowired MockMvc mockMvc) throws Exception {
MockHttpServletRequestBuilder builder=MockMvcRequestBuilders.get("/books");
ResultActions actions = mockMvc.perform(builder);
//设定预期值 与真实值进行比较,成功测试通过,失败则测试失败
//1.定义本次调用的预期值
ContentResultMatchers content = MockMvcResultMatchers.content();
ResultMatcher result = content.string("springboot");
//添加预期值到本次调用过程中进行匹配
actions.andExpect(result);
}
这里需要调用MockMvcResultMatchers的content方法返回响应体内容,然后调用ContentResultMathcers的string方法设置预期值,然后和真实值进行匹配。


浙公网安备 33010602011771号