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());
    }

}
相关推荐
消失的旧时光-194311 分钟前
第十四课 · 实战篇:Redis 缓存系统落地指南(Spring Boot 从 0 到可用)
spring boot·redis·缓存
wxin_VXbishe1 小时前
C#(asp.net)学员竞赛信息管理系统-计算机毕业设计源码28790
java·vue.js·spring boot·spring·django·c#·php
森焱森1 小时前
详解 Spring Boot、Flask、Nginx、Redis、MySQL 的关系与协作
spring boot·redis·python·nginx·flask
Coder_Boy_1 小时前
Deeplearning4j+ Spring Boot 电商用户复购预测案例
java·人工智能·spring boot·后端·spring
金牌归来发现妻女流落街头2 小时前
【Springboot基础开发】
java·spring boot·后端
是梦终空3 小时前
计算机毕业设计264—基于Springboot+Vue3+协同过滤的房屋租赁管理系统(源代码+数据库+万字论文+设计文档)
spring boot·毕业设计·vue3·课程设计·毕业论文·协同过滤·房屋租赁管理系统
qq_297574673 小时前
【实战】POI 实现 Excel 多级表头导出(含合并单元格完整方案)
java·spring boot·后端·excel
风流倜傥唐伯虎12 小时前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
fuquxiaoguang12 小时前
深入浅出:使用MDC构建SpringBoot全链路请求追踪系统
java·spring boot·后端·调用链分析