Spring运维之boo项目表现层测试匹配响应执行状态响应体JSON和响应头

匹配响应执行状态

我们创建了测试环境

而且发送了虚拟的请求

我们接下来要进行验证

验证请求和预期值是否匹配

MVC结果匹配器

匹配上了

匹配失败

package com.example.demo;

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.autoconfigure.web.servlet.MockMvcAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
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;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启spirngMVC的虚拟调用
@AutoConfigureMockMvc
public class WebTest {

    @Test
    void test(@Autowired MockMvc mvc) throws Exception {
        //创建了一个虚拟请求
        //get类型的请求 访问路径为books
        MockHttpServletRequestBuilder bulider= MockMvcRequestBuilders.get("/books");
        ResultActions action=mvc.perform(bulider);

        //设定预期值 与真实值进行比较 成功测试通过 失败测试不通过
        //定义本次调用的预期值
        StatusResultMatchers status=MockMvcResultMatchers.status();
        //预计本次调用成功 状态码200
        ResultMatcher ok=status.isOk();

        //添加预期值到本次调用过程中进行匹配
        action.andExpect(ok);
    }
}

小结

先去定义预期匹配结果

匹配响应体通解

匹配相应体是否和我们的预期结果一样

定义拿到的内容

package com.example.demo;

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.autoconfigure.web.servlet.MockMvcAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
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;
import org.springframework.test.web.servlet.result.StatusResultMatchers;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启spirngMVC的虚拟调用
@AutoConfigureMockMvc
public class WebTest {

    @Test
    void test(@Autowired MockMvc mvc) throws Exception {
        //创建了一个虚拟请求
        //get类型的请求 访问路径为books
        MockHttpServletRequestBuilder bulider= MockMvcRequestBuilders.get("/books");
        ResultActions action=mvc.perform(bulider);

        //设定预期值 与真实值进行比较 成功测试通过 失败测试不通过
        //定义本次调用的内容
        ContentResultMatchers content=MockMvcResultMatchers.content();

        //取到预计的内容
        ResultMatcher resultbody=content.string("springboot");

        //验证
        action.andExpect(resultbody);
    }
}

控制台输出打印信息

匹配JSON响应体

首先我们用lombok封装实体类

表现层把good数据发送到/goods路径上去

我们在页面中成功访问到

接下来我们要进行测试

如果匹配失败

package com.example.demo;

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.autoconfigure.web.servlet.MockMvcAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
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;
import org.springframework.test.web.servlet.result.StatusResultMatchers;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启spirngMVC的虚拟调用
@AutoConfigureMockMvc
public class WebTest {

    @Test
    void test(@Autowired MockMvc mvc) throws Exception {
        //创建了一个虚拟请求
        //get类型的请求 访问路径为books
        MockHttpServletRequestBuilder bulider= MockMvcRequestBuilders.get("/goods");
        ResultActions action=mvc.perform(bulider);

        //设定预期值 与真实值进行比较 成功测试通过 失败测试不通过
        //定义本次调用的内容
        ContentResultMatchers content=MockMvcResultMatchers.content();

        //取到预计的内容
        ResultMatcher resultbody=content.json("{\n" +
                "  \"id\": 1,\n" +
                "  \"name\": \"雨伞\",\n" +
                "  \"type\": \"日常用品\",\n" +
                "  \"description\": \"可以拿来防雨\"\n" +
                "}");

        //验证
        action.andExpect(resultbody);
    }
}

匹配响应头

package com.example.demo;

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.autoconfigure.web.servlet.MockMvcAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
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;
import org.springframework.test.web.servlet.result.StatusResultMatchers;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启spirngMVC的虚拟调用
@AutoConfigureMockMvc
public class WebTest {

    @Test
    void test(@Autowired MockMvc mvc) throws Exception {
        //创建了一个虚拟请求
        //get类型的请求 访问路径为books
        MockHttpServletRequestBuilder bulider= MockMvcRequestBuilders.get("/goods");
        ResultActions action=mvc.perform(bulider);

        //设定预期值 与真实值进行比较 成功测试通过 失败测试不通过
        //定义本次调用的内容
        HeaderResultMatchers header=MockMvcResultMatchers.header();

        //取到预计的内容
        ResultMatcher contentType=header.string("Content-Type","application/json");

        //验证
        action.andExpect(contentType);
    }
}
相关推荐
让学习成为一种生活方式10 分钟前
R包下载太慢安装中止的解决策略-R语言003
java·数据库·r语言
晨曦_子画16 分钟前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
Mephisto.java16 分钟前
【大数据学习 | kafka高级部分】kafka的优化参数整理
大数据·sql·oracle·kafka·json·database
成都古河云29 分钟前
智慧场馆:安全、节能与智能化管理的未来
大数据·运维·人工智能·安全·智慧城市
算法与编程之美31 分钟前
文件的写入与读取
linux·运维·服务器
南宫生38 分钟前
贪心算法习题其三【力扣】【算法学习day.20】
java·数据结构·学习·算法·leetcode·贪心算法
Heavydrink1 小时前
HTTP动词与状态码
java
ktkiko111 小时前
Java中的远程方法调用——RPC详解
java·开发语言·rpc
Amelio_Ming1 小时前
Permissions 0755 for ‘/etc/ssh/ssh_host_rsa_key‘ are too open.问题解决
linux·运维·ssh
心灵彼岸-诗和远方1 小时前
Devops业务价值流:软件研发最佳实践
运维·产品经理·devops