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

}
相关推荐
callJJ1 小时前
Spring Data Redis 两种编程模型详解:同步 vs 响应式
java·spring boot·redis·python·spring
海兰1 小时前
【第27篇】Micrometer + Zipkin
人工智能·spring boot·alibaba·spring ai
海兰3 小时前
【第28篇】可观测性实战:LangFuse 方案详解
人工智能·spring boot·alibaba·spring ai
RuoyiOffice4 小时前
SpringBoot+Vue3 企业考勤如何处理法定假期?节假日方案、调休补班与工作日判断链路拆解
spring boot·后端·vue·anti-design-vue·ruoyioffice·假期·人力
xmjd msup4 小时前
spring security 超详细使用教程(接入springboot、前后端分离)
java·spring boot·spring
952365 小时前
SpringBoot统一功能处理
java·spring boot·后端
rleS IONS5 小时前
SpringBoot中自定义Starter
java·spring boot·后端
TeDi TIVE6 小时前
springboot和springframework版本依赖关系
java·spring boot·后端
二哈赛车手6 小时前
新人笔记---ES和kibana启动问题以及一些常用的linux的错误排查方法,以及ES,数据库泄密解决方案[超详细]
java·linux·数据库·spring boot·笔记·elasticsearch
小编码上说8 小时前
LSH(局部敏感哈希)分桶,海量数据下的相似性搜索解决方案
java·spring boot·缓存·langchain4j·lsh·局部敏感哈希·ai调用优化