Spring Boot 提供了一个方便的测试工具类 MockMvc,用于对 Controller 层进行单元测试。MockMvc 提供了模拟 HTTP 请求的能力,让你可以在不启动完整的 HTTP 服务器的情况下测试 Spring MVC 控制器。
快速:MockMvc 可以在不启动完整的应用程序服务器的情况下进行单元测试,因此测试速度更快。
集成度高:它能够模拟 HTTP 请求和响应,以及 Spring MVC 的所有特性,如路由、过滤器、拦截器等。
灵活性:可以对控制器的行为进行精确的模拟和验证,包括请求参数、路径变量、请求头等。
在使用 MockMvc 进行单元测试时,你需要创建一个 MockMvc 实例,并使用它来构建和执行 HTTP 请求,然后验证返回结果。
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;
}
}
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());
}
}
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());
}
}