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

本文主要是介绍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 {@Testvoid test(@Autowired MockMvc mvc) throws Exception {//创建了一个虚拟请求//get类型的请求 访问路径为booksMockHttpServletRequestBuilder bulider= MockMvcRequestBuilders.get("/books");ResultActions action=mvc.perform(bulider);//设定预期值 与真实值进行比较 成功测试通过 失败测试不通过//定义本次调用的预期值StatusResultMatchers status=MockMvcResultMatchers.status();//预计本次调用成功 状态码200ResultMatcher 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 {@Testvoid test(@Autowired MockMvc mvc) throws Exception {//创建了一个虚拟请求//get类型的请求 访问路径为booksMockHttpServletRequestBuilder 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 {@Testvoid test(@Autowired MockMvc mvc) throws Exception {//创建了一个虚拟请求//get类型的请求 访问路径为booksMockHttpServletRequestBuilder 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 {@Testvoid test(@Autowired MockMvc mvc) throws Exception {//创建了一个虚拟请求//get类型的请求 访问路径为booksMockHttpServletRequestBuilder bulider= MockMvcRequestBuilders.get("/goods");ResultActions action=mvc.perform(bulider);//设定预期值 与真实值进行比较 成功测试通过 失败测试不通过//定义本次调用的内容HeaderResultMatchers header=MockMvcResultMatchers.header();//取到预计的内容ResultMatcher contentType=header.string("Content-Type","application/json");//验证action.andExpect(contentType);}
}

这篇关于Spring运维之boo项目表现层测试匹配响应执行状态响应体JSON和响应头的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1046341

相关文章

SpringBoot实现虚拟线程的方案

《SpringBoot实现虚拟线程的方案》Java19引入虚拟线程,本文就来介绍一下SpringBoot实现虚拟线程的方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录什么是虚拟线程虚拟线程和普通线程的区别SpringBoot使用虚拟线程配置@Async性能对比H

javaSE类和对象进阶用法举例详解

《javaSE类和对象进阶用法举例详解》JavaSE的面向对象编程是软件开发中的基石,它通过类和对象的概念,实现了代码的模块化、可复用性和灵活性,:本文主要介绍javaSE类和对象进阶用法的相关资... 目录前言一、封装1.访问限定符2.包2.1包的概念2.2导入包2.3自定义包2.4常见的包二、stati

SpringBoot结合Knife4j进行API分组授权管理配置详解

《SpringBoot结合Knife4j进行API分组授权管理配置详解》在现代的微服务架构中,API文档和授权管理是不可或缺的一部分,本文将介绍如何在SpringBoot应用中集成Knife4j,并进... 目录环境准备配置 Swagger配置 Swagger OpenAPI自定义 Swagger UI 底

解决hive启动时java.net.ConnectException:拒绝连接的问题

《解决hive启动时java.net.ConnectException:拒绝连接的问题》Hadoop集群连接被拒,需检查集群是否启动、关闭防火墙/SELinux、确认安全模式退出,若问题仍存,查看日志... 目录错误发生原因解决方式1.关闭防火墙2.关闭selinux3.启动集群4.检查集群是否正常启动5.

SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南

《SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南》本文将基于开源项目springboot-easyexcel-batch进行解析与扩展,手把手教大家如何在SpringBo... 目录项目结构概览核心依赖百万级导出实战场景核心代码效果百万级导入实战场景监听器和Service(核心

idea Maven Springboot多模块项目打包时90%的问题及解决方案

《ideaMavenSpringboot多模块项目打包时90%的问题及解决方案》:本文主要介绍ideaMavenSpringboot多模块项目打包时90%的问题及解决方案,具有很好的参考价值,... 目录1. 前言2. 问题3. 解决办法4. jar 包冲突总结1. 前言之所以写这篇文章是因为在使用Mav

Spring Security6.3.x的使用指南与注意事项

《SpringSecurity6.3.x的使用指南与注意事项》SpringSecurity6.3.1基于现代化架构,提供简洁配置、增强默认安全性和OAuth2.1/OIDC支持,采用Lambda... 目录介绍基础配置 (Servlet 应用 - 使用 Lambda DSL)关键配置详解(Lambda DS

Java Stream 的 Collectors.toMap高级应用与最佳实践

《JavaStream的Collectors.toMap高级应用与最佳实践》文章讲解JavaStreamAPI中Collectors.toMap的使用,涵盖基础语法、键冲突处理、自定义Map... 目录一、基础用法回顾二、处理键冲突三、自定义 Map 实现类型四、处理 null 值五、复杂值类型转换六、处理

基于Python Playwright进行前端性能测试的脚本实现

《基于PythonPlaywright进行前端性能测试的脚本实现》在当今Web应用开发中,性能优化是提升用户体验的关键因素之一,本文将介绍如何使用Playwright构建一个自动化性能测试工具,希望... 目录引言工具概述整体架构核心实现解析1. 浏览器初始化2. 性能数据收集3. 资源分析4. 关键性能指

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1