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

SpringBoot开发实用篇 - 测试 - 匹配响应执行状态,匹配响应体(json版),匹配响应头

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

SpringBoot开发实用篇 - 测试 - 匹配响应执行状态,匹配响应体(json版),匹配响应头

文章目录
  • 匹配响应执行状态
    • 案例模拟
      • 编写WebTest.java
      • 测试运行
    • 小结
  • 匹配响应体(了解)
    • 案例模拟
      • 编写WebTest.java
      • 测试运行
    • 小结
  • 匹配响应体(json)
    • 案例模拟
      • 创建User实体类
      • 编辑UserController.java
        • 运行测试
      • 编辑WebTest.java
      • 测试运行
    • 小结
  • 匹配响应头
    • 案例模拟
      • 编写WebTest.java
      • 运行测试
    • 小结
  • 总结如何使用测试响应

匹配响应执行状态 案例模拟 编写WebTest.java

更改urlTemplate为错误地址

package com.taotao;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.result.StatusResultMatchers;


@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
@AutoConfigureMockMvc
public class WebTest {
    @Test
    void test(){

    }

    @Test
    void testWeb(@Autowired MockMvc mvc) throws Exception {
        //创建虚拟请求,当前访问/Users
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/Users1");
        //执行对应的请求
        ResultActions actions = mvc.perform(builder);

        //设定预期值 与真实值进行比较,成功测试通过,失败测试失败
        StatusResultMatchers status = MockMvcResultMatchers.status();
        //预计本次调用时成功的:状态200
        ResultMatcher ok = status.isOk();
        //添加预计值到本次调用过程中进行匹配
        actions.andExpect(ok);
    }
}
测试运行

小结

匹配响应体(了解)

更改expectedContent属性为错误响应体

案例模拟 编写WebTest.java
package com.taotao;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.ContentResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;


@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
@AutoConfigureMockMvc
public class WebTest {
    @Test
    void test(){

    }

    @Test
    void testBody(@Autowired MockMvc mvc) throws Exception {
        //创建虚拟请求,当前访问/Users
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/Users");
        //执行对应的请求
        ResultActions actions = mvc.perform(builder);
        //设定预期值 与真实值进行比较,成功测试通过,失败测试失败
        //定义本次的预期值
        ContentResultMatchers content = MockMvcResultMatchers.content();
        //预计本次调用时成功的:状态200
        ResultMatcher result = content.string("springboot1");
        //添加预计值到本次调用过程中进行匹配
        actions.andExpect(result);
    }
}

测试运行

小结

匹配响应体(json) 案例模拟 创建User实体类
package com.taotao.domain;

import lombok.Data;


@SuppressWarnings({"all"})
@Data
public class User {
    private int id;
    private String name;
    private int age;
    private String  address;
}
编辑UserController.java
package com.taotao.controller;

import com.taotao.domain.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@SuppressWarnings({"all"})
@RestController
@RequestMapping("/Users")
public class UserController {
    @GetMapping
    public User getById() {
        System.out.println("getById is running");
        User user = new User();
        user.setId(1);
        user.setName("涛涛");
        user.setAge(18);
        user.setAddress("河南");
        return user;
    }
}
运行测试

运行Springboot14TestApplication.java

编辑WebTest.java

返回的json是上面的测试通过返回的json数据

package com.taotao;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.ContentResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;


@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
@AutoConfigureMockMvc
public class WebTest {
    @Test
    void test(){

    }

    @Test
    void testJosn(@Autowired MockMvc mvc) throws Exception {
        //创建虚拟请求,当前访问/Users
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/Users");
        //执行对应的请求
        ResultActions actions = mvc.perform(builder);
        //设定预期值 与真实值进行比较,成功测试通过,失败测试失败
        //定义本次的预期值
        ContentResultMatchers content = MockMvcResultMatchers.content();
        //预计本次调用时成功的:状态200
        ResultMatcher json = content.json("{"id":1,"name":"" +
                "涛涛","age":18,"address":"河南"}");
        //添加预计值到本次调用过程中进行匹配
        actions.andExpect(json);
    }
}
测试运行

成功

更改错误的json串,测试运行

小结

匹配响应头 案例模拟 编写WebTest.java

String参数和value参数都在这

package com.taotao;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.ContentResultMatchers;
import org.springframework.test.web.servlet.result.HeaderResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;


@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
@AutoConfigureMockMvc
public class WebTest {
    @Test
    void test(){

    }

    @Test
    void testJosn(@Autowired MockMvc mvc) throws Exception {
        //创建虚拟请求,当前访问/Users
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/Users");
        //执行对应的请求
        ResultActions actions = mvc.perform(builder);
        //设定预期值 与真实值进行比较,成功测试通过,失败测试失败
        //定义本次的预期值
        HeaderResultMatchers header = MockMvcResultMatchers.header();
        //预计本次调用时成功的:状态200
        ResultMatcher string = header.string("Content-Type", "application/json");
        //添加预计值到本次调用过程中进行匹配
        actions.andExpect(string);
    }
}
运行测试

成功

修改数据,失败

小结

总结如何使用测试响应

如果时间足够,所有测试写在一起,直接全测

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

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

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