java springboot测试类虚拟MVC环境 匹配返回值与预期内容是否相同 (JSON数据格式) 版

上文java springboot测试类鉴定虚拟MVC请求 返回内容与预期值是否相同我们讲了测试类中 虚拟MVC发送请求 匹配返回内容是否与预期值相同 但是 让我意外的是 既然没人骂我 因为我们实际开发 返回的基本都是json数据 字符串的接口场景是少数的

我们在java文件目录下创建一个 domain 文件夹

下面创建一个user类

参考代码如下

java 复制代码
package com.example.webdom.domain;

public class user {
    private int id;
    private String name;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

这边 我就设置一下最基本的 id和name 然后声明一下对应的 get set函数

这边 我们 controller 代码更改如下

java 复制代码
package com.example.webdom.controller;

import com.example.webdom.domain.user;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/TextWeb")
public class TestWeb {
    @GetMapping
    public user getById(){
        user user = new user();
        user.setId(1);
        user.setName("数据管理");
        System.out.println("getById is running .....");
        return user;
    }
}

这里 我们直接 new 一个 user类对象 然后 set一下他的id和name

然后接口返回这个对象出去

将测试类 代码改写如下

java 复制代码
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 {

    @Test
    void contextLoads(@Autowired MockMvc mvc) throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/TextWeb");
        ResultActions action = mvc.perform(builder);
        ContentResultMatchers content = MockMvcResultMatchers.content();
        ResultMatcher result = content.json("{\"id\":1,\"name\":\"数据管理\"}");
        action.andExpect(result);
    }

}

这里 我们因为还是判断内容 所以依旧用content

然后 里面写一个json格式的字符串即可

然后 我们右键测试函数运行

返回的json和这个json串是一样的 自然不会 有什么问题

我们重点还是要看错误的 这里 我爸 name 后面加一个1 让他匹配不上

然后 我们再次右键运行 出错 是我们想要的

这个位置的内容依旧这么给力 依旧告诉你了 到底是那个字段出问题了 name

然后告诉了你区别 可以说 非常只能了

相关推荐
Java探秘者5 分钟前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea
攸攸太上6 分钟前
Spring Gateway学习
java·后端·学习·spring·微服务·gateway
2301_7869643611 分钟前
3、练习常用的HBase Shell命令+HBase 常用的Java API 及应用实例
java·大数据·数据库·分布式·hbase
2303_8120444614 分钟前
Bean,看到P188没看了与maven
java·开发语言
苹果醋315 分钟前
大模型实战--FastChat一行代码实现部署和各个组件详解
java·运维·spring boot·mysql·nginx
秋夫人17 分钟前
idea 同一个项目不同模块如何设置不同的jdk版本
java·开发语言·intellij-idea
m0_6640470222 分钟前
数字化采购管理革新:全过程数字化采购管理平台的架构与实施
java·招投标系统源码
潘多编程24 分钟前
Spring Boot与GraphQL:现代化API设计
spring boot·后端·graphql
aqua353574235841 分钟前
蓝桥杯-财务管理
java·c语言·数据结构·算法
Deryck_德瑞克42 分钟前
Java网络通信—TCP
java·网络·tcp/ip