栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot基础(25):匹配请求结果

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot基础(25):匹配请求结果

文章目录
  • 前言
  • 匹配执行状态
  • 匹配body
  • 匹配json
  • 匹配header

前言

测试用例匹配响应结果

匹配执行状态

状态匹配

@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc //开启mvc调用
public class WebTest {

    //通过形参方式注入Mvc对象
    @Test
    void hello2(@Autowired MockMvc mockMvc) throws Exception {
       //创建请求,http://localhost:xxxx/hello
        MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/hello2");
        ResultActions action=mockMvc.perform(builder);

        StatusResultMatchers status= MockMvcResultMatchers.status(); //定义预期执行状态
        ResultMatcher ok=status.isOk();//设定的预期状态
        //将请求结果与预期结果比对
        action.andExpect(ok);
    }
}

执行结果

(更改了请求链接)失败的结果

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /hello2
       Parameters = {}
          Headers = []
             Body = null
    Session Attrs = {}

Handler:
             Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 404
    Error message = null
          Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers"]
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []


java.lang.AssertionError: Status expected:<200> but was:<404>
Expected :200
Actual   :404

匹配body
 //通过形参方式注入Mvc对象
    @Test
    void hello3(@Autowired MockMvc mockMvc) throws Exception {
        //创建请求,http://localhost:xxxx/hello
        MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/hello");
        ResultActions action=mockMvc.perform(builder);

        ContentResultMatchers content= MockMvcResultMatchers.content();
        ResultMatcher ok=content.string("hello");
        //将请求结果与预期结果比对
        action.andExpect(ok);

    }

匹配返回结果与预期是否一致

匹配json
//创建请求,http://localhost:xxxx/hello
        MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/getById");
        ResultActions action=mockMvc.perform(builder);

        ContentResultMatchers content= MockMvcResultMatchers.content();
        ResultMatcher result=content.json("{"id":1,"name":"wangsan"}");
        //将请求结果与预期结果比对
        action.andExpect(result);

结果
略

匹配header
 //创建请求,http://localhost:xxxx/hello
        MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/getById");
        ResultActions action=mockMvc.perform(builder);

        HeaderResultMatchers header= MockMvcResultMatchers.header();
        ResultMatcher result=header.string("Content-Type","application/json");//匹配响应头
        //将请求结果与预期结果比对
        action.andExpect(result);

结果
略

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1000033.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号