SpringBoot测试类启动web环境-下篇

一、响应状态

1.MockMvcResultMatchers

说明:模拟结果匹配。

java 复制代码
package com.forever;

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.RequestBuilder;
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.MockMvcResultMatchers;
import org.springframework.test.web.servlet.result.StatusResultMatchers;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启虚拟mvc调用
@AutoConfigureMockMvc
public class WebTest {

    @Test
        //形参写入或者上方写@Autowired;为方法注入资源。
    void testWeb(@Autowired MockMvc mockMvc) throws Exception {
        //发请求http://....../users;下面就是模拟的一个http请求,访问的是users
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users1");
        //执行对应的请求。
      ResultActions action= mockMvc.perform(builder);
        //设定预期的值进行比较,成功测试通过,失败测试失败。
        //定义本次调用的预期值
        StatusResultMatchers status = MockMvcResultMatchers.status();
        //预计本次调用时成功的:状态200
        ResultMatcher ok = status.isOk();
        //添加预期值到本次调用中进行匹配
        action.andExpect(ok);

    }


}

2.匹配失败

二、响应体

1.content方法

java 复制代码
 @Test
        //形参写入或者上方写@Autowired;为方法注入资源。
    void testBody(@Autowired MockMvc mockMvc) throws Exception {
        //发请求http://....../users;下面就是模拟的一个http请求,访问的是users
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users");
        //执行对应的请求。
        ResultActions action = mockMvc.perform(builder);
        //设定预期的值进行比较,成功测试通过,失败测试失败。
        //定义本次调用的预期值
        ContentResultMatchers content = MockMvcResultMatchers.content();

        ResultMatcher resBody = content.string("springboot-good");
        //添加预期值到本次调用中进行匹配
        action.andExpect(resBody);

    }

2.匹配失败

三、json格式

1.实体类

java 复制代码
package com.forever.domain;

import lombok.Data;

@Data
public class User {
    private  int id;
    private  String name;
    private  int age;
}

2.控制层

java 复制代码
package com.forever;

import com.forever.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("/users")
public class UserController {
    @GetMapping
    public User getUser() {
        System.out.println("getById is running ....");
        User user = new User();
        user.setName("李四");
        user.setAge(18);
        user.setId(1);
        return user;
    }
}

3. 测试方法

说明:content.json()里面放的是json对象。

java 复制代码
    @Test
        //形参写入或者上方写@Autowired;为方法注入资源。
    void testJson(@Autowired MockMvc mockMvc) throws Exception {
        //发请求http://....../users;下面就是模拟的一个http请求,访问的是users
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users");
        //执行对应的请求。
        ResultActions action = mockMvc.perform(builder);
        //设定预期的值进行比较,成功测试通过,失败测试失败。
        //定义本次调用的预期值
        ContentResultMatchers content = MockMvcResultMatchers.content();

        ResultMatcher resBody = content.json("");
        //添加预期值到本次调用中进行匹配
        action.andExpect(resBody);

    }

四、content-type

1.测试方法

java 复制代码
    @Test
        //形参写入或者上方写@Autowired;为方法注入资源。
    void testContentType(@Autowired MockMvc mockMvc) throws Exception {
        //发请求http://....../users;下面就是模拟的一个http请求,访问的是users
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users");
        //执行对应的请求。
        ResultActions action = mockMvc.perform(builder);
        //设定预期的值进行比较,成功测试通过,失败测试失败。
        //定义本次调用的预期值
        HeaderResultMatchers header  = MockMvcResultMatchers.header();

      ResultMatcher contentType=header.string("Content-good","application/json");
        //添加预期值到本次调用中进行匹配
        action.andExpect(contentType);

    }

}

2.匹配错误

相关推荐
それども9 分钟前
Spring Bean 的name可以相同吗
java·后端·spring
上进小菜猪12 分钟前
基于深度学习的农业虫害自动识别系统:YOLOv8 的完整工程
后端
曹牧17 分钟前
C#:记录日志
服务器·前端·c#
小飞侠在吗19 分钟前
Vue customRef
前端·javascript·vue.js
xhxxx38 分钟前
别再让 AI 自由发挥了!用 LangChain + Zod 强制它输出合法 JSON
前端·langchain·llm
指尖跳动的光1 小时前
判断页签是否为活跃状态
前端·javascript·vue.js
用泥种荷花1 小时前
【前端学习AI】大模型调用实战
前端
Lan.W1 小时前
element UI + vue2 + html实现堆叠条形图 - 横向分段器
前端·ui·html
FAQEW1 小时前
若依(RuoYi-Vue)单体架构实战手册:自定义业务模块全流程开发指南
前端·后端·架构·若依二开
神算大模型APi--天枢6461 小时前
合规与高效兼得:国产全栈架构赋能行业大模型定制,从教育到工业的轻量化落地
大数据·前端·人工智能·架构·硬件架构