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

相关推荐
神奇小汤圆22 分钟前
字节二面:10亿数据毫秒级查手机尾号后4位,答不出“异构索引”直接挂?
后端
IT_Octopus26 分钟前
Spring Boot 线程池关闭:destroyMethod 的作用与最佳实践
java·spring boot·后端
Leslie16526 分钟前
用 Reactor 和 Linux 命名管道实现一个本地事件总线
后端
幼儿园技术家31 分钟前
原来不用发版也可以做到版本更新
前端·js or ts
亦暖筑序1 小时前
AgentScope-Java 入门:完善 Vue 前端、发布 GitHub,并规划下一步
java·前端·vue.js
北斗落凡尘1 小时前
Vue面试题
前端
一只叫煤球的猫1 小时前
ThreadForge 源码解读三:从任务执行到并发编排,ScopeJoiner 是怎么工作的?
后端·性能优化·开源
程序员黑豆1 小时前
鸿蒙应用开发之父子组件传参:@Param、@Event、@Once 装饰器详解与实战
前端·harmonyos
ClouGence1 小时前
一个人录好的测试用例,团队怎么一起用?
前端·测试
无限压榨切图仔1 小时前
从 Claude Code 切到 Codex:我用 Agent、Skills、MCP 做完了一个内容运营工具
前端·后端