在上一篇,项目基本实现了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

Spring Boot 2.x使用Mockito进行测试的更多相关文章

  1. Spring Boot项目中使用Mockito

    本文首发于个人网站:Spring Boot项目中使用Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试 ...

  2. Spring Boot -05- 多模块结构项目构建与测试(详细图文教程)IDEA 版

    Spring Boot -05- 多模块结构项目构建与测试(详细图文教程)IDEA 版 百度很多博客都不详细,弄了半天才把 Spring Boot 多模块项目构建开发整的差不多,特地重新创建配置,记录 ...

  3. spring boot使用java读取配置文件,DateSource测试,BomCP测试,AnnotationConfigApplicationContext的DataSource注入

    一.配置注解读取配置文件         (1)@PropertySource可以指定读取的配置文件,通过@Value注解获取值   实例:           @PropertySource(val ...

  4. Spring Boot教程(十)异步方法测试

    测试 测试代码如下: @Component public class AppRunner implements CommandLineRunner { private static final Log ...

  5. Spring Boot应用的测试——Mockito

    Spring Boot应用的测试——Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试:Spring ...

  6. Spring Boot 1.4测试的改进

    对Pivotal团队来说,工作上的好事情是他们拥有一个被叫做Pivotal Labs的灵活发展部门,拥有Labs团队的Lean 和 XP程序设计方法学的强大支持,例如结对编程和测试驱动开发.他们对于测 ...

  7. spring boot test中mockito的运用

    mock的意义 在微服务盛行的当下,开发过程中往往出现A应用中某功能的实现需要调用B应用的接口,无论使用RPC还是restful都需要B应用提供接口的实现整个开发工作才能继续进行.从而导致A应用的开发 ...

  8. 在Spring Boot项目中使用Spock测试框架

    本文首发于个人网站:在Spring Boot项目中使用Spock测试框架 Spock框架是基于Groovy语言的测试框架,Groovy与Java具备良好的互操作性,因此可以在Spring Boot项目 ...

  9. Spring Boot 解决方案 - JUnit 测试

    简单的 JUnit 项目 回顾一下创建并运行简单的 JUnit 测试项目,先添加 JUnit 依赖然后编写类似如下模板的测试类,使用 IDE 的话直接用插件运行就行, 使用 Maven 的话运行命令 ...

随机推荐

  1. python2与python3爬虫中get与post对比

    python2中的urllib2改为python3中的urllib.request 四种方式对比: python2的get: # coding=utf-8 import urllib import u ...

  2. Directx11教程(14) D3D11管线(3)

    原文:Directx11教程(14) D3D11管线(3)       现在我们开始学习一些CP(command processor)的知识.参考资料: http://fgiesen.wordpres ...

  3. oracle审计的格式

    audit table; audit table by xxx(username); audit table by xxx(username) whenever not successful; 系统表 ...

  4. js错误处理Try-catch和throw

    1.try-catch语句   Try{ //可能会导致错误的代码 }catch(error){ //在错误发生时怎么处理 } 例如: try{ window.someNonexistentFunct ...

  5. Nginx 缓存代理

    访问ArcGIS官网的地图瓦片太慢.想到可用NIGIX代理. Nginx是Linux下http代理软件,Windows下也有. 以下为配置方法,注意红色部分. 1.需要在本地proxy_cache_p ...

  6. javascript中数字的一些常规操作

    1,禁止输入 - (减号.负号) // html <input type="number" class="no-negative"> // js $ ...

  7. spring的父子关系

    1.父容器不能拿子容器的资源 2.子容器可以拿到父容器的资源

  8. LeetCode Weekly Contest 6

    leetcode现在每周末举办比赛,这是今天上午参加的比赛的题解.题目难度不算大,两个easy,一个medium,一个hard.hard题之前接触过,所以做得比较顺利. 1.  Sum of Left ...

  9. 详解Python中内置的NotImplemented类型的用法

    它是什么? ? 1 2 >>> type(NotImplemented) <type 'NotImplementedType'> NotImplemented 是Pyth ...

  10. Linux 运算符

    布尔运算符 下表列出了常用的布尔运算符,假定变量a为10 变量b为20: 运算符  说明 举例 ! 非运算 , 表达式为true 则返回false 否则返回true  [!false] 返回true. ...