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

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

相关推荐
半部论语3 分钟前
Spring **${}** vs **#{}** 语法全景图
java·数据库·spring boot·后端·spring
sql2008help6 分钟前
数据分页异步后台导出excel
java·excel
知行合一。。。7 分钟前
Spring--04--2--AOP自定义注解,数据过滤处理
java·后端·spring
wuxuanok11 分钟前
八股——Kafka相关
java·笔记·后端·学习·kafka
天天摸鱼的java工程师17 分钟前
MyBatis SQL 耗时记录的拦截器实战
java·后端·面试
oraen22 分钟前
kraft的设计与实现
java·kafka
ZeroNews内网穿透1 小时前
ZeroNews内网穿透安全策略深度解析:构建企业级安全连接体系
java·运维·服务器·网络·python·安全·php
麦兜*1 小时前
Spring Integration 整合 Web3.0网关:智能合约事件监听与Spring Integration方案
java·spring boot·后端·spring·spring cloud·web3·智能合约
回家路上绕了弯1 小时前
分布式缓存实战指南:从架构到落地的完整方案
java·后端
日月星辰Ace1 小时前
🚀 从压测到优化:用 k6 和 SpringBoot 构建高性能系统(附实战案例)
spring boot·后端·性能优化