Spring Boot 2.x使用Mockito进行测试

在上一篇,项目基本实现了Spring Boot对Mybatis的整合。这篇文章使用Mockito对项目进行测试。

1、使用postmat测试;

2、编写单元测试类,使用mockito进行测试;

3、使用idea内置工具进行测试

运行AicodeBgmsApplication.java,启动项目后,可以采用如下方式对接口进行测试。

一、使用postman进行测试

如上图所示进行测试,其他接口请自行测试。

二、编写单元测试类进行测试

这里使用Idea辅助我们创建单元测试类

在要测试的类,如:UserInfoController.java类中点击右键,再点击Go To,再点击Test,如下图所示:

或者点击菜单上的Navigate,然后点击Test,选择Create New Test...

然后进入下面界面,如下:

确定要测试的类和包路径,把编写单元测试的方法都选中,然后点击OK。单元测试类即可生成。

编写的测试代码如下:UserInfoControllerTest.java

  1. package com.aicode.bgms.controller;
  2. import org.junit.After;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import org.springframework.test.web.servlet.MockMvc;
  10. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
  11. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import org.springframework.util.LinkedMultiValueMap;
  14. import org.springframework.util.MultiValueMap;
  15. import org.springframework.web.context.WebApplicationContext;
  16. import static org.junit.Assert.*;
  17. @RunWith(SpringRunner.class)
  18. @Transactional
  19. @SpringBootTest
  20. public class UserInfoControllerTest {
  21. private MockMvc mockMvc;
  22. @Autowired
  23. private WebApplicationContext wac;
  24. @Before
  25. public void setUp() throws Exception {
  26. this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
  27. }
  28. @After
  29. public void tearDown() throws Exception {
  30. }
  31. @Test
  32. public void list() throws Exception {
  33. String mvcResult= mockMvc.perform(MockMvcRequestBuilders.get("/list"))
  34. .andReturn().getResponse().getContentAsString();
  35. System.out.println("Result === "+mvcResult);
  36. }
  37. @Test
  38. public void add() throws Exception {
  39. final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
  40. params.add("userName", "test2");
  41. params.add("password", "pass1234");
  42. params.add("age", "12");
  43. params.add("email", "test@aicode.com");
  44. String mvcResult= mockMvc.perform(MockMvcRequestBuilders.post("/add")
  45. .params(params)).andReturn().getResponse().getContentAsString();
  46. System.out.println("Result === "+mvcResult);
  47. }
  48. @Test
  49. public void get() throws Exception {
  50. String mvcResult= mockMvc.perform(MockMvcRequestBuilders.get("/get/1"))
  51. .andReturn().getResponse().getContentAsString();
  52. System.out.println("Result === "+mvcResult);
  53. }
  54. @Test
  55. public void modify() throws Exception {
  56. final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
  57. params.add("id", "1");
  58. params.add("userName", "test1");
  59. params.add("password", "123qwe");
  60. params.add("age", "24");
  61. params.add("email", "test@aicode.com");
  62. String mvcResult= mockMvc.perform(MockMvcRequestBuilders.put("/edit")
  63. .params(params)).andReturn().getResponse().getContentAsString();
  64. System.out.println("Result === "+mvcResult);
  65. }
  66. @Test
  67. public void del() throws Exception {
  68. mockMvc.perform(MockMvcRequestBuilders.delete("/del/2"))
  69. .andReturn();
  70. String mvcResult= mockMvc.perform(MockMvcRequestBuilders.get("/list"))
  71. .andReturn().getResponse().getContentAsString();
  72. System.out.println("Result === "+mvcResult);
  73. }
  74. }
  • @SpringBootTest —— SpringBoot 自 1.4.0 版本开始引入的一个用于测试的注解;
  • @RunWith(SpringRunner.class) ——代表运行一个 Spring 容器;
  • @Transactional——可以使单元测试进行事务回滚,以保证数据库表中没有因测试造成的垃圾数据,再就是保证单元测试可以反复执行;
  • @Before—— 代表在测试启动时候需要提前加载的内容,这里是提前加载 MVC 环境。

执行UserInfoControllerTest.java,然后下面是执行单元测试的结果

关于MockMvc进行单元测试如果不太清楚,可以先百度一下做一下基本了解。

可以点击每个测试方法,查看具体的测试结果

这样,我们使用MockMvc就完成了对Controller层的测试。Service层和Dao层也可以使用MockMvc进行测试,这里就不再进行说明,请读者自行尝试。

三、使用Idea中的工具进行测试

运行AicodeBgmsApplication.java,启动项目。

点击菜单中的Tools——〉Test Restful Web Service,然后在窗口输入参数进行测试。

添加:

点击左侧的绿色右向三角标运行,然后可以看到返回结果。

查询:

点击左侧的绿色右向三角标运行,然后可以看到返回结果。

测试时,请求HTTP method的匹配。

原文地址:https://blog.csdn.net/zhenbie/article/details/84072282
原文地址:https://www.cnblogs.com/jpfss/p/10973302.html