对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. linux如何查看服务器当前的并发访问量

    linux如何查看服务器当前的并发访问量 [root@localhost ~]# netstat -pnt | grep :80 | wc -l 2 [root@localhost ~]# netst ...

  2. jenkins学习9-测试报告发邮件(Email Extension Plugin)

    前言 前面已经实现在jenkins上展示html的测试报告,接下来只差最后一步,把报告发给你的领导,展示你的劳动成果了. 安装 Email Extension Plugin 插件 jenkins首页- ...

  3. 51 Nod 1083 矩阵取数问题(动态规划)

    原题链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1083 题目分析:通过读题发现我们只能往右边或者下边走,意味着 ...

  4. springboot打包第三方jar包是失败

    在项目开发时有时我们需要引入一些在maven仓库中不存在的包 一.配置maven环境变量 在path环境变量中添加 %maven_home%\bin (window10环境下) cmd界面输入 mvn ...

  5. Sentry 开发者贡献指南 - 数据库迁移

    Django 迁移是我们处理 Sentry 中数据库更改的方式. Django 迁移官方文档:https://docs.djangoproject.com/en/2.2/topics/migratio ...

  6. 通过HTML+CSS+Javascript实现向下滚动滚动条出现导航栏并出现回到顶部按钮点击按钮回到顶部(一)

    回到顶部实例一 效果:默认隐藏导航栏,当滚动条滚到超过300px后导航栏和按钮出现,点击回到顶部按钮回到顶部,并隐藏导航栏和按钮(导航栏和按钮都是固定定位) <!doctype html> ...

  7. 白嫖党的福音!!!全新的Java300集视频(2022版)来了!

    它来了它来了,经过一年时间的沉淀, [尚学堂]高淇Java300集完整版正式发布啦! 应广大网友和尚学堂忠实的孜孜学子以及听众朋友的要求,尚学堂在去年十月份就把预计在2022年发布的Java300集提 ...

  8. elasticsearch启动流程

    本文基于ES2.3.2来描述.通过结合源码梳理出ES实例的启动过程. elasticsearch的启动过程是根据配置和环境组装需要的模块并启动的过程.这一过程就是通过guice注入各个功能模块并启动这 ...

  9. 使用TortoiseGit和Git Bash不需要输入RSA密码(passphrase)的方法

    1. 安装和配置Putty 安装 官网下载Putty并安装. 生成ppk密钥 打开puttygen.exe(C:\Program Files\PuTTY\puttygen.exe), 点Convers ...

  10. java-异常-异常应用

    1 package p1.exception; 2 3 4 /* 5 * 老师用电脑上课. 6 * 7 * 问题领域中涉及两个对象. 8 * 老师,电脑. 9 * 10 * 分析其中的问题. 11 * ...