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.匹配错误

相关推荐
veneno1 天前
大量异步并发请求控制并发解决方案
前端
3***C7441 天前
Spring Boot 整合 log4j2 日志配置教程
spring boot·单元测试·log4j
X***C8621 天前
SpringBoot:几种常用的接口日期格式化方法
java·spring boot·后端
i***t9191 天前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
oden1 天前
2025博客框架选择指南:Hugo、Astro、Hexo该选哪个?
前端·html
小光学长1 天前
基于ssm的宠物交易系统的设计与实现850mb48h(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·前端·数据库
8***84821 天前
spring security 超详细使用教程(接入springboot、前后端分离)
java·spring boot·spring
o***74171 天前
基于SpringBoot的DeepSeek-demo 深度求索-demo 支持流式输出、历史记录
spring boot·后端·lua
9***J6281 天前
Spring Boot项目集成Redisson 原始依赖与 Spring Boot Starter 的流程
java·spring boot·后端
小小前端要继续努力1 天前
渐进增强、优雅降级及现代Web开发技术详解
前端