SpringBoot MockMvc

SpringBoot MockMvc

  • [1 什么是 MockMvc?](#1 什么是 MockMvc?)
  • [2 为什么使用 MockMvc?](#2 为什么使用 MockMvc?)
  • [3 如何使用 MockMvc?](#3 如何使用 MockMvc?)
    • [3.1 Controller](#3.1 Controller)
    • [3.2 测试方法一](#3.2 测试方法一)
    • [3.3 测试方法二](#3.3 测试方法二)

1 什么是 MockMvc?

Spring Boot 提供了一个方便的测试工具类 MockMvc,用于对 Controller 层进行单元测试。MockMvc 提供了模拟 HTTP 请求的能力,让你可以在不启动完整的 HTTP 服务器的情况下测试 Spring MVC 控制器。

2 为什么使用 MockMvc?

快速:MockMvc 可以在不启动完整的应用程序服务器的情况下进行单元测试,因此测试速度更快。

集成度高:它能够模拟 HTTP 请求和响应,以及 Spring MVC 的所有特性,如路由、过滤器、拦截器等。

灵活性:可以对控制器的行为进行精确的模拟和验证,包括请求参数、路径变量、请求头等。

3 如何使用 MockMvc?

在使用 MockMvc 进行单元测试时,你需要创建一个 MockMvc 实例,并使用它来构建和执行 HTTP 请求,然后验证返回结果。

3.1 Controller

java 复制代码
package com.xu.test.controller;

import com.xu.test.service.TestService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private TestService testService;


    @RequestMapping("/test1")
    public String test1(String a, Integer b) {
        return testService.test1(a, b);
    }

    @RequestMapping("/test2")
    public Object test2(HttpServletRequest request, HttpServletResponse response, String a, Integer b) {
        testService.test2(request, response, a, b);
        Map<String, Object> body = new HashMap<>();
        body.put("a", a);
        body.put("b", b);
        return body;
    }

}

3.2 测试方法一

java 复制代码
package com.xu.test;

import cn.hutool.json.JSONUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
@RunWith(SpringRunner.class)
public class MockMvcTest1 {

    private MockMvc mock;

    @Autowired
    private WebApplicationContext wac;

    @Before
    public void setUp() {
        mock = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void get() throws Exception {
        ResultActions actions = mock.perform(MockMvcRequestBuilders.get("/test/test1?a=1&b=2")
                .header("X-Access-Token", "0123456789")
                .cookie(new Cookie("cookie", "123456789"))
                .param("a", "1")
                .param("b", "2"));

        // 期望请求成功
        actions.andExpect(MockMvcResultMatchers.status().isOk());
        // 打印请求头
        actions.andDo(MockMvcResultHandlers.print());

        MvcResult result = actions.andReturn();
        // 断言
        Assert.assertTrue(result.getResponse().isCommitted());
    }

    @Test
    public void post() throws Exception {
        // 请求体
        Map<String, Object> body = new HashMap<>();
        body.put("a", "1");

        // 请求
        ResultActions actions = mock.perform(MockMvcRequestBuilders.post("/test/test2")
                .header("X-Access-Token", "0123456789")
                .header(HttpHeaders.AUTHORIZATION, "0123456789")
                .content(JSONUtil.toJsonPrettyStr(body))
                .param("a", "1")
                .param("b", "2")
                .contentType(MediaType.APPLICATION_JSON_VALUE));

        // 期望返回的是JSON
        actions.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
        // 期望返回的a=1
        actions.andExpect(MockMvcResultMatchers.jsonPath("$.a").value(1));
        // 期望请求成功
        actions.andExpect(MockMvcResultMatchers.status().isOk());
        // 打印请求头
        actions.andDo(MockMvcResultHandlers.print());
        // 结果
        MvcResult result = actions.andReturn();
        // 断言
        Assert.assertEquals(MediaType.APPLICATION_JSON_VALUE, result.getResponse().getContentType());
    }

}

3.3 测试方法二

java 复制代码
package com.xu.test;

import com.xu.test.controller.TestController;
import com.xu.test.service.TestService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import javax.servlet.http.Cookie;

@RunWith(SpringRunner.class) // 声明使用 SpringRunner 进行测试
@WebMvcTest(TestController.class) // 声明需要测试的 Controller 类
public class MockMvcTest3 {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private TestService testService;

    @Test
    public void test1() throws Exception {
        ResultActions actions = mockMvc.perform(MockMvcRequestBuilders.get("/test/test1")
                .header("X-Access-Token", "0123456789")
                .cookie(new Cookie("cookie", "123456789"))
                .param("a", "1")
                .param("b", "2"));

        // 期望请求成功
        actions.andExpect(MockMvcResultMatchers.status().isOk());
        // 打印请求头
        actions.andDo(MockMvcResultHandlers.print());
        // 结果
        MvcResult result = actions.andReturn();
        // 断言
        Assert.assertTrue(result.getResponse().isCommitted());
    }

}
相关推荐
小马爱打代码1 小时前
Spring Boot:Sentinel 企业级熔断、降级与限流实战
spring boot·后端·sentinel
long3161 小时前
合并排序 merge sort
java·数据结构·spring boot·算法·排序算法
李慕婉学姐1 小时前
【开题答辩过程】以《基于SpringBoot Vue的校园后勤管理系统设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
vue.js·spring boot·后端
咖啡啡不加糖1 小时前
Arthas 使用指南:Java 应用诊断利器
java·spring boot·后端
大佐不会说日语~1 小时前
Docker Compose 部署 Spring Boot 应用 502 Bad Gateway 问题排查与解决
spring boot·docker·gateway·maven·故障排查
J_liaty1 小时前
SpringBoot整合Canal实现数据库实时同步
数据库·spring boot·后端·canal
小北方城市网1 小时前
MongoDB 分布式存储与查询优化:从副本集到分片集群
java·spring boot·redis·分布式·wpf
bjxiaxueliang3 小时前
一文掌握SpringBoot:HTTP服务开发从入门到部署
spring boot·后端·http
过期动态15 小时前
Java开发中的@EnableWebMvc注解和WebMvcConfigurer接口
java·开发语言·spring boot·spring·tomcat·maven·idea
我爱娃哈哈16 小时前
SpringBoot + Flowable + 自定义节点:可视化工作流引擎,支持请假、报销、审批全场景
java·spring boot·后端