Spring Boot实战之单元测试

本文介绍使用Spring测试框架提供的MockMvc对象,对Restful API进行单元测试

Spring测试框架提供MockMvc对象,可以在不需要客户端-服务端请求的情况下进行MVC测试,完全在服务端这边就可以执行Controller的请求,跟启动了测试服务器一样。

测试开始之前需要建立测试环境,setup方法被@Before修饰。通过MockMvcBuilders工具,使用WebApplicationContext对象作为参数,创建一个MockMvc对象。

MockMvc对象提供一组工具函数用来执行assert判断,都是针对web请求的判断。这组工具的使用方式是函数的链式调用,允许程序员将多个测试用例链接在一起,并进行多个判断。在这个例子中我们用到下面的一些工具函数:

perform(get(...))建立web请求。在我们的第三个用例中,通过MockMvcRequestBuilder执行GET请求。

andExpect(...)可以在perform(...)函数调用后多次调用,表示对多个条件的判断,这个函数的参数类型是ResultMatcher接口,在MockMvcResultMatchers这这个类中提供了很多返回ResultMatcher接口的工具函数。这个函数使得可以检测同一个web请求的多个方面,包括HTTP响应状态码(response status),响应的内容类型(content type),会话中存放的值,检验重定向、model或者header的内容等等。这里需要通过第三方库json-path检测JSON格式的响应数据:检查json数据包含正确的元素类型和对应的值,例如jsonPath("$.name").value("中文测试")用于检查在根目录下有一个名为name的节点,并且该节点对应的值是“testuser”。

本文对rest api的开发不做详细描述,如需了解可以参考 Spring Boot实战之Rest接口开发及数据库基本操作

1、修改pom.xml,添加依赖库json-path,用于检测JSON格式的响应数据


  1. <dependency>
  2. <groupId>com.jayway.jsonpath</groupId>
  3. <artifactId>json-path</artifactId>
  4. </dependency>

2、添加用户数据模型UserInfo.java


  1. package com.xiaofangtech.sunt.bean;
  2. import javax.persistence.Entity;
  3. import javax.persistence.GeneratedValue;
  4. import javax.persistence.GenerationType;
  5. import javax.persistence.Id;
  6. import javax.persistence.Table;
  7. import javax.validation.constraints.Size;
  8. @Entity
  9. @Table(name="t_userinfo")
  10. public class UserInfo {
  11. @Id
  12. @GeneratedValue(strategy = GenerationType.AUTO)
  13. private Long id;
  14. @Size(min=0, max=32)
  15. private String name;
  16. private Integer age;
  17. @Size(min=0, max=255)
  18. private String address;
  19. public Long getId() {
  20. return id;
  21. }
  22. public void setId(Long id) {
  23. this.id = id;
  24. }
  25. public String getName() {
  26. return name;
  27. }
  28. public void setName(String name) {
  29. this.name = name;
  30. }
  31. public Integer getAge() {
  32. return age;
  33. }
  34. public void setAge(Integer age) {
  35. this.age = age;
  36. }
  37. public String getAddress() {
  38. return address;
  39. }
  40. public void setAddress(String address) {
  41. this.address = address;
  42. }
  43. }

3、添加控制器UserController.java,用于实现对用户的增删改查


  1. package com.xiaofangtech.sunt.controller;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.data.jpa.repository.Modifying;
  5. import org.springframework.web.bind.annotation.RequestBody;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import com.xiaofangtech.sunt.bean.UserInfo;
  10. import com.xiaofangtech.sunt.repository.UserInfoRepository;
  11. import com.xiaofangtech.sunt.utils.*;
  12. @RestController
  13. @RequestMapping("user")
  14. public class UserController {
  15. @Autowired
  16. private UserInfoRepository userRepositoy;
  17. /***
  18. * 根据用户id,获取用户信息
  19. * @param id
  20. * @return
  21. */
  22. @RequestMapping(value="getuser", method=RequestMethod.GET)
  23. public Object getUser(Long id)
  24. {
  25. UserInfo userEntity = userRepositoy.findOne(id);
  26. ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), userEntity);
  27. return resultMsg;
  28. }
  29. /***
  30. * 获取所有用户列表
  31. * @return
  32. */
  33. @RequestMapping(value="getalluser", method=RequestMethod.GET)
  34. public Object getUserList()
  35. {
  36. List<UserInfo> userEntities = (List<UserInfo>) userRepositoy.findAll();
  37. ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), userEntities);
  38. return resultMsg;
  39. }
  40. /***
  41. * 新增用户信息
  42. * @param userEntity
  43. * @return
  44. */
  45. @Modifying
  46. @RequestMapping(value="adduser", method=RequestMethod.POST)
  47. public Object addUser(@RequestBody UserInfo userEntity)
  48. {
  49. userRepositoy.save(userEntity);
  50. ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), userEntity);
  51. return resultMsg;
  52. }
  53. /***
  54. * 更新用户信息
  55. * @param userEntity
  56. * @return
  57. */
  58. @Modifying
  59. @RequestMapping(value="updateuser", method=RequestMethod.PUT)
  60. public Object updateUser(@RequestBody UserInfo userEntity)
  61. {
  62. UserInfo user = userRepositoy.findOne(userEntity.getId());
  63. if (user != null)
  64. {
  65. user.setName(userEntity.getName());
  66. user.setAge(userEntity.getAge());
  67. user.setAddress(userEntity.getAddress());
  68. userRepositoy.save(user);
  69. }
  70. ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), user);
  71. return resultMsg;
  72. }
  73. /***
  74. * 删除用户
  75. * @param id
  76. * @return
  77. */
  78. @Modifying
  79. @RequestMapping(value="deleteuser", method=RequestMethod.DELETE)
  80. public Object deleteUser(Long id)
  81. {
  82. try
  83. {
  84. userRepositoy.delete(id);
  85. }
  86. catch(Exception exception)
  87. {
  88. }
  89. ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), null);
  90. return resultMsg;
  91. }
  92. }

4、修改测试类,添加对以上接口进行单元测试的测试用例


  1. package com.xiaofangtech.sunt;
  2. import org.junit.Before;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.SpringApplicationConfiguration;
  7. import org.springframework.http.MediaType;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9. import org.springframework.test.context.web.WebAppConfiguration;
  10. import org.springframework.test.web.servlet.MockMvc;
  11. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  12. import org.springframework.web.context.WebApplicationContext;
  13. import com.fasterxml.jackson.databind.ObjectMapper;
  14. import com.xiaofangtech.sunt.bean.UserInfo;
  15. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  16. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  17. import static org.hamcrest.Matchers.*;
  18. //这是JUnit的注解,通过这个注解让SpringJUnit4ClassRunner这个类提供Spring测试上下文。
  19. @RunWith(SpringJUnit4ClassRunner.class)
  20. //这是Spring Boot注解,为了进行集成测试,需要通过这个注解加载和配置Spring应用上下
  21. @SpringApplicationConfiguration(classes = SpringJUnitTestApplication.class)
  22. @WebAppConfiguration
  23. public class SpringJUnitTestApplicationTests {
  24. @Autowired
  25. private WebApplicationContext context;
  26. private MockMvc mockMvc;
  27. @Before
  28. public void setupMockMvc() throws Exception {
  29. mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
  30. }
  31. /***
  32. * 测试添加用户接口
  33. * @throws Exception
  34. */
  35. @Test
  36. public void testAddUser() throws Exception
  37. {
  38. //构造添加的用户信息
  39. UserInfo userInfo = new UserInfo();
  40. userInfo.setName("testuser2");
  41. userInfo.setAge(29);
  42. userInfo.setAddress("北京");
  43. ObjectMapper mapper = new ObjectMapper();
  44. //调用接口,传入添加的用户参数
  45. mockMvc.perform(post("/user/adduser")
  46. .contentType(MediaType.APPLICATION_JSON_UTF8)
  47. .content(mapper.writeValueAsString(userInfo)))
  48. //判断返回值,是否达到预期,测试示例中的返回值的结构如下{"errcode":0,"errmsg":"OK","p2pdata":null}
  49. .andExpect(status().isOk())
  50. .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
  51. //使用jsonPath解析返回值,判断具体的内容
  52. .andExpect(jsonPath("$.errcode", is(0)))
  53. .andExpect(jsonPath("$.p2pdata", notNullValue()))
  54. .andExpect(jsonPath("$.p2pdata.id", not(0)))
  55. .andExpect(jsonPath("$.p2pdata.name", is("testuser2")));
  56. }
  57. /***
  58. * 测试更新用户信息接口
  59. * @throws Exception
  60. */
  61. @Test
  62. public void testUpdateUser() throws Exception
  63. {
  64. //构造添加的用户信息,更新id为2的用户的用户信息
  65. UserInfo userInfo = new UserInfo();
  66. userInfo.setId((long)2);
  67. userInfo.setName("testuser");
  68. userInfo.setAge(26);
  69. userInfo.setAddress("南京");
  70. ObjectMapper mapper = new ObjectMapper();
  71. mockMvc.perform(put("/user/updateuser")
  72. .contentType(MediaType.APPLICATION_JSON_UTF8)
  73. .content(mapper.writeValueAsString(userInfo)))
  74. //判断返回值,是否达到预期,测试示例中的返回值的结构如下
  75. //{"errcode":0,"errmsg":"OK","p2pdata":null}
  76. .andExpect(status().isOk())
  77. .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
  78. .andExpect(jsonPath("$.errcode", is(0)))
  79. .andExpect(jsonPath("$.p2pdata", notNullValue()))
  80. .andExpect(jsonPath("$.p2pdata.id", is(2)))
  81. .andExpect(jsonPath("$.p2pdata.name", is("testuser")))
  82. .andExpect(jsonPath("$.p2pdata.age", is(26)))
  83. .andExpect(jsonPath("$.p2pdata.address", is("南京")));
  84. }
  85. /***
  86. * 测试根据用户id获取用户信息接口
  87. * @throws Exception
  88. */
  89. @Test
  90. public void testGetUser() throws Exception
  91. {
  92. mockMvc.perform(get("/user/getuser?id=2"))
  93. .andExpect(status().isOk())
  94. .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
  95. .andExpect(jsonPath("$.errcode", is(0)))
  96. .andExpect(jsonPath("$.p2pdata", notNullValue()))
  97. .andExpect(jsonPath("$.p2pdata.id", is(2)))
  98. .andExpect(jsonPath("$.p2pdata.name", is("testuser")))
  99. .andExpect(jsonPath("$.p2pdata.age", is(26)))
  100. .andExpect(jsonPath("$.p2pdata.address", is("南京")));
  101. }
  102. /***
  103. * 测试获取用户列表接口
  104. * @throws Exception
  105. */
  106. @Test
  107. public void testGetUsers() throws Exception
  108. {
  109. mockMvc.perform(get("/user/getalluser"))
  110. .andExpect(status().isOk())
  111. .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
  112. .andExpect(jsonPath("$.errcode", is(0)))
  113. .andExpect(jsonPath("$.p2pdata", notNullValue()));
  114. }
  115. }

5、运行测试,执行JUnit Test

一共执行4个测试用例,全都通过

原文地址:https://blog.csdn.net/sun_t89/article/details/52185952

Spring Boot实战之单元测试的更多相关文章

  1. Spring Boot实战系列-----------邮件发送

    快速导航 添加Maven依赖 配置文件增加邮箱相关配置 Service.Test项目代码构建 五种邮件发送类型讲解 文本邮件 html邮件 附件邮件 html内嵌图片邮件 模板邮件 问题汇总 添加ma ...

  2. spring boot实战(第十三篇)自动配置原理分析

    前言 spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理. 在上一篇末尾讲述了Spring ...

  3. spring boot实战(第十二篇)整合RabbitMQ

    前言 最近几篇文章将围绕消息中间件RabbitMQ展开,对于RabbitMQ基本概念这里不阐述,主要讲解RabbitMQ的基本用法.Java客户端API介绍.spring Boot与RabbitMQ整 ...

  4. 《spring boot 实战》读书笔记

    前言:虽然已经用spring boot开发过一套系统,但是之前都是拿来主义,没有系统的,全面的了解过这套框架.现在通过学习<spring boot实战>这本书,希望温故知新.顺便实现自己的 ...

  5. Spring Boot 实战与原理分析视频课程

    Spring Boot 实战与原理分析视频课程 链接:https://pan.baidu.com/share/init?surl=PeykcoeqZtd1d9lN9V_F-A 提取码: 关注公众号[G ...

  6. spring boot实战(第一篇)第一个案例

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   spring boot实战(第一篇)第一个案例 前言 写在前面的话 一直想将spring boot相关内容写成一个系列的 ...

  7. [转] Spring Boot实战之Filter实现使用JWT进行接口认证

    [From] http://blog.csdn.net/sun_t89/article/details/51923017 Spring Boot实战之Filter实现使用JWT进行接口认证 jwt(j ...

  8. 【spring boot】10.spring boot下的单元测试

    spring boot下的单元测试,思前想后还是需要单独用一章篇幅来看看. 然后在看了介绍和使用时候,我感觉并不想多去看了. 但是还是给后来人留下参考的路径: 官网说明:https://spring. ...

  9. 9.Spring Boot实战之配置使用Logback进行日志记录

    转自:https://blog.csdn.net/meiliangdeng1990/article/details/54300227 Spring Boot实战之配置使用Logback进行日志记录 在 ...

随机推荐

  1. SPSS能做Cochran-Armitage趋势检验吗

    SPSS能做Cochran-Armitage趋势检验吗 Cochran-Armitage (CA) 趋势检验是一种用于分析1个二分类变量和1个有序分类变量关联性的统计方法,由Cochran和Armti ...

  2. 【JZOJ4923】【NOIP2017提高组模拟12.17】巧克力狂欢

    题目描述 Alice和Bob有一棵树(无根.无向),在第i个点上有ai个巧克力.首先,两人个选择一个起点(不同的),获得点上的巧克力:接着两人轮流操作(Alice先),操作的定义是:在树上找一个两人都 ...

  3. oracle dbms_repcat_admin能带来什么安全隐患

    如果一个用户能执行dbms_repcat_admin包,将获得极大的系统权限. 以下情况可能获得该包的执行权限: 1.在sys下grant execute on dbms_repcat_admin t ...

  4. 2019-9-2-win10-uwp-右击浮出窗在点击位置

    title author date CreateTime categories win10 uwp 右击浮出窗在点击位置 lindexi 2019-09-02 12:57:38 +0800 2018- ...

  5. 如何实现两个input框的同步更新

    class MVVM { constructor(options) { this.$options = options; this.$el = options.el; this.$data = opt ...

  6. Hadoop应用程序示例2

  7. linux内核分析笔记----中断和中断处理程序

    中断还是中断,我讲了很多次的中断了,今天还是要讲中断,为啥呢?因为在操作系统中,中断是必须要讲的.. 那么什么叫中断呢, 中断还是打断,这样一说你就不明白了.唉,中断还真是有点像打断.我们知道linu ...

  8. 6、mysql的安装

    1.安装mysql-Server,并输入root密码 sudo apt-get install mysql-server 2.安装mysql客户端 sudo apt-get install mysql ...

  9. Project Euler Problem 24-Lexicographic permutations

    全排列的生成,c++的next_permutation是O(n)生成全排列的.具体的O(n)生成全排列的算法,在 布鲁迪 的那本组合数学中有讲解(课本之外,我就看过这一本组合数学),冯速老师翻译的,具 ...

  10. H3C 分页显示