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

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

相关推荐
不想写bug呀8 分钟前
多线程案例——单例模式
java·开发语言·单例模式
心平愈三千疾39 分钟前
通俗理解JVM细节-面试篇
java·jvm·数据库·面试
我不会写代码njdjnssj42 分钟前
网络编程 TCP UDP
java·开发语言·jvm
第1缕阳光1 小时前
Java垃圾回收机制和三色标记算法
java·jvm
funnyZpC1 小时前
好用的文档工具👉smart-doc
java
考虑考虑1 小时前
@FilterRegistration和@ServletRegistration注解
spring boot·后端·spring
一只叫煤球的猫1 小时前
🔥 同事混用@Transactional和TransactionTemplate被我怼了,三种事务管理到底怎么选?
java·spring boot·后端
华子w9089258599 天前
基于 SpringBoot+JSP 的医疗预约与诊断系统设计与实现
java·spring boot·后端
feifeigo1239 天前
Java 正则表达式高级用法
java·mysql·正则表达式
empti_9 天前
Java中的List实现类详解
java