对Controller进行单元测试是Spring框架原生就支持的能力,它可以模拟HTTP客户端发起对服务地址的请求,可以不用借助于诸如Postman这样的外部工具就能完成对接口的测试。

具体来讲,是由Spring框架中的spring-test模块提供的实现,详见MockMvc

如下将详细阐述如何使用MockMvc测试框架实现对“Spring Controller”进行单元测试,基于Spring Boot开发框架进行验证。

添加测试框架依赖:

<!-- Spring框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
!<-- Spring测试框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 文件操作工具 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>

导入静态工具方法

为了便于在编写测试用例时直接调用测试框架自带的静态方法,首先需要导入这些静态工具方法。

需要导入的静态方法如下:

import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.setup.SharedHttpSessionConfigurer.*;

初始化MockMvc

初始化MockMvc有2种方式:

方式1:明确指定需要测试的“Controller”类进行配置

方式2:基于Spring容器进行配置,包含了Spring MVC环境和所有“Controller”类,通常使用这种方式。

@SpringBootTest
public class TestControllerTest { MockMvc mockMvc; // 初始化MockMvc
@BeforeEach
void setUp(WebApplicationContext wac) {
// 方式1:明确指定需要测试的“Controller”类
this.mockMvc = MockMvcBuilders.standaloneSetup(new TestController()).build(); // 方式2:基于Spring容器进行配置,包含了Spring MVC环境和所有“Controller”类。
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
}

另外,还可以对MockMvc进行全局配置。

// 全局配置MockMvc
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac)
.defaultRequest(get("/").accept(MediaType.APPLICATION_JSON)) // 默认请求路径
.apply(sharedHttpSession()) // 配置session
.alwaysExpect(status().isOk()) // 预期响应状态码
.alwaysExpect(content().contentType("application/json;charset=UTF-8")) // 预期内容类型
.build();

执行测试

MockMvc支持对常见的HTTP方法,如:GET,POST,PUT,DELETE等,甚至还支持文件上传请求。

测试GET接口

// 访问GET接口:不带参数
@Test
public void testSimpleGet() throws Exception {
MvcResult result = this.mockMvc.perform(get("/test/simple/get")
.accept(MediaType.APPLICATION_JSON)) // 接受JSON格式响应消息
.andReturn(); // 获取返回结果
Assertions.assertEquals("OK", result.getResponse().getContentAsString());
} // 访问GET接口:带URL参数
@Test
public void testParamGet() throws Exception {
int id = 10;
// 方式1:在URI模板中指定参数
//MvcResult result = this.mockMvc.perform(get("/test/param/get?id={id}", id).accept(MediaType.APPLICATION_JSON)).andReturn(); // 方式2:通过param()方法指定参数
//MvcResult result = this.mockMvc.perform(get("/test/param/get").param("id", String.valueOf(id)).accept(MediaType.APPLICATION_JSON)).andReturn(); // 方式3:通过queryParam()方法指定参数
MvcResult result = this.mockMvc.perform(get("/test/param/get").queryParam("id", String.valueOf(id)).accept(MediaType.APPLICATION_JSON)).andReturn();
Assertions.assertEquals("OK: " + id, result.getResponse().getContentAsString());
}

测试POST接口

// 传递表单参数
@Test
public void testSimplePost() throws Exception {
int id = 10; // 调用param()方法传递参数
MvcResult result = this.mockMvc.perform(post("/test/simple/post")
.param("id", String.valueOf(id))
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.accept(MediaType.APPLICATION_JSON))
.andReturn();
Assertions.assertEquals("{\"id\":10}", result.getResponse().getContentAsString());
} // 传递JSON参数
@Test
public void testSimplePostJson() throws Exception {
// 调用content()方法传递json字符串参数
Subject subject = new Subject();
subject.setId(10);
String content = JSON.toJSONString(subject);
MvcResult result = this.mockMvc.perform(post("/test/simple/post/json")
.content(content)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andReturn();
Assertions.assertEquals("{\"id\":10}", result.getResponse().getContentAsString());
}

测试文件上传

@Test
public void testFileUploadSingle() throws Exception {
File file = new File("C:\\Users\\xxx\\Downloads\\test.jpg");
String fileName = FilenameUtils.getName(file.getName());
byte[] bytes = FileUtils.readFileToByteArray(file);
MockMultipartFile mockMultipartFile = new MockMultipartFile("file", fileName, MediaType.MULTIPART_FORM_DATA_VALUE, bytes);
this.mockMvc.perform(multipart("/test/upload/single").file(mockMultipartFile))
.andExpect(status().isOk())
.andExpect(content().string("OK"))
.andDo(print());
}

定义预期结果

断言响应结果时,有2种方式:

1.使用JUnit提供的Assert断言工具判断返回结果,这是一种非常普遍和常见的方式

2.在MockMvc框架中可以通过andExpect()方法定义一个或多个预期结果,当其中一个期望结果断言失败时,就不会断言其他期望值了

// 使用Junit断言工具判断返回结果是否符合预期
@Test
public void testAssertResult() throws Exception {
MvcResult result = this.mockMvc.perform(get("/test/simple/get").accept(MediaType.APPLICATION_JSON)).andDo(print()).andReturn();
Assert.assertEquals("OK", result.getResponse().getContentAsString());
} // 在MockMvc框架中定义预期结果
@Test
public void testExpectations() throws Exception {
this.mockMvc.perform(get("/test/simple/get").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) // 预期响应状态码为200
.andExpect(content().string("OK")) // 预期返回值为字符串“OK”
.andDo(print());
}

相比于使用Junit的断言工具判断返回结果,在MockMvc框架中直接定义预期结果进行断言检查更加简洁。

写在最后

使用Spring提供的测试框架MockMvc可以非常方便地实现对HTTP服务接口进行单元测试,不要把基础的功能验证工作都交给测试童鞋,应该通过单元测试来保证代码迭代的稳定性。

【参考】

https://blog.csdn.net/coolcoffee168/article/details/88638042 springboot 单元测试 (controller层) 方法 -- MockMvc

如何对Spring MVC中的Controller进行单元测试的更多相关文章

  1. Spring MVC中的Controller是Serlvet吗?

    1. Controller不是Servlet DispatcherServler是Spring MVC中的唯一Servlet,(这点可通过查看FrameworkServlet的子类确认) Servle ...

  2. spring mvc中的文件上传

    使用commons-fileupload上传文件所需要的架包有:commons-fileupload 和common-io两个架包支持,可以到Apache官网下砸. 在配置文件spring-mvc.x ...

  3. Spring MVC中一般 普通类调用service

    在Spring MVC中,Controller中使用service只需使用注解@Resource就行,但是一般类(即不使用@Controller注解的类)要用到service时,可用如下方法: 1.S ...

  4. Spring MVC中一般类使用service

    在Spring MVC中,Controller中使用service只需使用注解@Resource就行,但是一般类(即不使用@Controller注解的类)要用到service时,可用如下方法: 1.S ...

  5. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  6. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

  7. Spring MVC中,事务是否可以加在Controller层

    一般而言,事务都是加在Service层的,但是爱钻牛角尖的我时常想:事务加在Controller层可不可以.我一直试图证明事务不止可以加在Service层,还可以加在Controller层,但是没有找 ...

  8. Spring MVC 中的基于注解的 Controller(转载)

           终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法 ...

  9. Http请求中Content-Type讲解以及在Spring MVC中的应用

    引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...

随机推荐

  1. Centos7上传文件和下载文件命令

    https://www.cnblogs.com/patrick-yeh/p/12922829.html 使用工具:SecureCRT 或 Xshell 步骤一:安装lrzsz软件,root权限下.安装 ...

  2. Git 如何放弃所有本地修改

    git checkout . #本地所有的修改,没有提交的,都返回到原来的状态 git stash #把所有没有提交的修改暂存到stash里面.可用git stash pop恢复. git reset ...

  3. spring security 动态 修改当前登录用户的 权限

    1.前言 spring security 可以获取当前登录的用户信息,同时提供了接口 来修改权限列表信息 , 使用这个方法 ,可以动态的修改当前登录用户权限. 那么问题来了... 如果我是管理员 ,如 ...

  4. Word2010初识

    原文链接:https://www.toutiao.com/i6487370439910752782/ 认识Word Microsoft Office Word是微软公司的一个文字处理器应用软件. 启动 ...

  5. vue3.0+vite+ts项目搭建-postcss-pxtorem 实现移动自适应(五)

    这里不考虑大屏,所以不做amfe-flexible的配置 首先是安装依赖 yarn add postcss-loader postcss-pxtorem -D yarn add autoprefixe ...

  6. .net core 和 WPF 开发升讯威在线客服系统:调用百度翻译接口实现实时自动翻译

    业余时间用 .net core 写了一个在线客服系统.并在博客园写了一个系列的文章,写介绍这个开发过程. 我把这款业余时间写的小系统丢在网上,陆续有人找我要私有化版本,我都给了,毕竟软件业的初衷就是免 ...

  7. 方法覆盖 和toString方法的作用

    当我们代码怎么编写的时候,在代码级别上构成了方法的覆盖呢? 两个类必须要有继承关系. 重写之后的方法和之前的方法具有:相同的返回值类型 相同的方法名 相同的形参列表 访问权限不能更高,只能更低 重写之 ...

  8. Underscore.js 1.3.3 源码分析收藏

    Underscore是一个提供许多函数编程功能的库,里面包含了你期待(在Prototype.js和Ruby中)的许多功能.但是没有扩展任何内置的Javascript对象,也就是说它没有扩展任何内置对象 ...

  9. 学习Flutter从0开始

    一. 认识Flutter 1.1. 什么是Flutter 先看看官方的解释: Flutter is Google's UI toolkit for building beautiful, native ...

  10. No shutdown animation in the electricity display only 1%

    低电量自动关机时无关机动画 低电量自动关机时无关机动画1. 问题描述2. 分析3. solution4. 总结 1. 问题描述 DEFECT DESCRIPTION: No shutdown anim ...