建设银行网站开通查询密码,扶风做网站,在线推广企业网站的方法是,百度推广官网入口上文java springboot测试类鉴定虚拟MVC请求 返回内容与预期值是否相同我们讲了测试类中 虚拟MVC发送请求 匹配返回内容是否与预期值相同 但是 让我意外的是 既然没人骂我 因为我们实际开发 返回的基本都是json数据 字符串的接口场景是少数的 我们在java文件目录下创建一个 dom…上文java springboot测试类鉴定虚拟MVC请求 返回内容与预期值是否相同我们讲了测试类中 虚拟MVC发送请求 匹配返回内容是否与预期值相同 但是 让我意外的是 既然没人骂我 因为我们实际开发 返回的基本都是json数据 字符串的接口场景是少数的 我们在java文件目录下创建一个 domain 文件夹 下面创建一个user类
参考代码如下
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 代码更改如下
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 {GetMappingpublic user getById(){user user new user();user.setId(1);user.setName(数据管理);System.out.println(getById is running .....);return user;}
}这里 我们直接 new 一个 user类对象 然后 set一下他的id和name 然后接口返回这个对象出去
将测试类 代码改写如下
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.json({\id\:1,\name\:\数据管理\});action.andExpect(result);}}这里 我们因为还是判断内容 所以依旧用content 然后 里面写一个json格式的字符串即可
然后 我们右键测试函数运行 返回的json和这个json串是一样的 自然不会 有什么问题 我们重点还是要看错误的 这里 我爸 name 后面加一个1 让他匹配不上 然后 我们再次右键运行 出错 是我们想要的 这个位置的内容依旧这么给力 依旧告诉你了 到底是那个字段出问题了 name 然后告诉了你区别 可以说 非常只能了