Spring Boot实战之单元测试

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,用于实现对用户的增删改查

扫描二维码关注公众号,回复: 6348023 查看本文章

   
   
  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

猜你喜欢

转载自www.cnblogs.com/jpfss/p/10973610.html
今日推荐