当前位置: 首页 > news >正文

java springboot测试类鉴定虚拟MVC请求 返回内容与预期值是否相同

上文 java springboot测试类鉴定虚拟MVC运行值与预期值是否相同
中 我们验证了它HTTP的返回状态 简单说 校验了他 是否成功的状态
这次 我们来不对得到的内容
我们 直接改写测试类代码如下

package com.example.webdom;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;@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class WebDomApplicationTests {@Testvoid contextLoads(@Autowired MockMvc mvc) throws Exception {MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/TextWeb");ResultActions action = mvc.perform(builder);ContentResultMatchers content = MockMvcResultMatchers.content();ResultMatcher result = content.string("springboot");action.andExpect(result);}}

这次 我们声明 content 表示要比对内容
然后 content.string中设置预期内容 最后比对
和我们上文的比对格式基本是一致的 只是换了写关键字

这里 我们直接运行
在这里插入图片描述
这边运行就通过了 因为我们最后返回的确实是个 springboot
在这里插入图片描述
那么 这里 我们将匹配内容改一下
这里 我们改个错的 然后重新运行
在这里插入图片描述
我们可以看到 这里他就抛异常了 里面会告诉你 预期是springboot
但实际得到的是 springboot2
在这里插入图片描述

http://www.lryc.cn/news/240280.html

相关文章:

  • MongoDB随记
  • 839 - Not so Mobile (UVA)
  • php字符串处理函数的使用
  • UEC++ day8
  • 学习记录——ipv4、ipv6与ip、DNS、网络协议
  • cefsharp119.4.30(cef119.4.3,Chromium119.0.6045.159)版本升级体验支持H264及其他多个H264版本
  • “index“ should always be multi-word
  • 服务器64GB内存、8核CPU的MySQL 8配置参数
  • Python+Qt虹膜检测识别
  • 我的创作纪念日——365天
  • 安卓手机便签APP用哪个,手机上好用的便签APP是什么
  • 前端Date对象的使用锦集
  • 如何将ONLYOFFICE与Python应用程序集成
  • vector的简单模拟实现_C++
  • 合并两个有序链表,剑指offer,力扣
  • Delphi 12 Athens 发布了!
  • 基于Haclon的Blob分析
  • 安卓手机好用的清单软件有哪些?
  • 【追求卓越02】数据结构--链表
  • qt按照不同编码格式读取文字(UTF-16LE,UTF-8,UTF-8BOM,UTF-16BE)
  • R语言和RStudio的下载安装(非常简便舒适)
  • SQL注入漏洞发现和利用,以及SQL注入的防护
  • Jmeter 分布式压测
  • Docker 安装 Apache
  • python变量、常量、数据类型
  • 注册中心CAP架构剖析
  • SVN创建分支
  • Vue 设置v-html中元素样式
  • 连接服务器的脚本
  • ChatGPT/GPT4丨编程助手;AI画图;数据分析;科研/项目实现;提示词工程技巧;论文写作等