1.之前开发项目是不要求写单测的,最近公司管理严格需要对开发的功能编写单测,所以在此记录下springboot对junit的整合以及使用的方式

2.引入需要用到的依赖jar包,一般创建好springboot项目都会自带test依赖

3.一般我们新建的springboot项目都带有测试包,我们直接使用,在里面编写测试类即可

4.因为项目中可能会存在很多测试类,那么就会存在很多注解重复被添加的冗余,因此我们写一个基类,其他测试类只需要继承基类就行,基类名字就叫做BaseTestClass,如下:

package com.example.mybatisplus;import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;@SpringBootTest
@WebAppConfiguration
@RunWith(SpringRunner.class)
public class BaseTestClass {@Beforepublic void init(){System.out.println("before");}@Afterpublic void after(){System.out.println("after");}
}

5.然后就可以编写测试类了,测试类里面注入service层,然后通过断言进行返回数据的判断,下面就是我测试的一个查询接口的测试类:

package com.example.mybatisplus;import com.example.mybatisplus.entity.Employee;
import com.example.mybatisplus.service.EmployeeService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;import static org.junit.Assert.assertEquals;public class OperateDataTest extends BaseTestClass{@Autowiredprivate EmployeeService employeeService;@Testpublic void testQueryById(){Employee employee = employeeService.selectById("2");assertEquals("Jerry",employee.getLastName());assertEquals("jerry@qq.com",employee.getEmail());}
}

注意:由于业务逻辑层都是要求写在service层,所以我们这里就注入了service进行测试。

那有同学要问了,我直接测试controller层可以吗?答案肯定是可以的,下面我就演示一下直接注入controller层,其实和service是一样的,如下所示:

package com.example.mybatisplus;import com.example.mybatisplus.controller.MybatisPlusController;
import com.example.mybatisplus.entity.Employee;
import com.example.mybatisplus.service.EmployeeService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;import java.util.List;import static org.junit.Assert.assertEquals;public class OperateDataTest extends BaseTestClass{@Autowiredprivate EmployeeService employeeService;@Autowiredprivate MybatisPlusController mybatisPlusController;@Testpublic void testQueryById(){Employee employee = employeeService.selectById("2");assertEquals("Jerry",employee.getLastName());assertEquals("jerry@qq.com",employee.getEmail());}@Testpublic void testController(){List<Employee> employeeList = mybatisPlusController.queryList();System.out.println("employee="+employeeList.get(0));}
}

项目代码已上传csdn,下载链接:springboot整合junit测试用例demo-Java文档类资源-CSDN下载

SpringBoot整合junit测试案例相关推荐

  1. SpringBoot整合Junit测试

    文章目录 SpringBoot整合Junit测试 1.SpringBoot引入springboot的测试依赖 2.生成测试方法 3.测试结果 SpringBoot整合Junit测试 假设已对mybat ...

  2. Java高级特性 第10节 IDEA和Eclipse整合JUnit测试框架

    一.IDEA整合Junit测试框架 1.安装插件 打开File菜单的下拉菜单settings[设置] : 点击左侧Plugins[插件]菜单 在输入框中输入JUnitGenerator 2.0,点击I ...

  3. springboot整合swagger+mybatisplus案例

    1.前后端分离的一个常用的文档接口swaggerui越来越受欢迎,方便了前端以及后端人员的测试 2.如下为springboot整合swagger和mybatispus案例的github地址:https ...

  4. springboot整合mybatis-plus完整案例

    1.mybatis-plus,plus意思为插件,只在原来的基础上增强不做改变,只要进行简单的配置就可以对单表进行crud,还提供了强大的条件构造器wrapper以及代码生成器等.如下为springb ...

  5. SpringBoot集成JUnit测试

    在一些企业的实践中,要求开发人员编写测试编码来测试业务逻辑,以提高编码的质量.降低错误的发生概率以及进行性能测试等.这些IDE在创建Spring Boot应用的时候已经引入了测试包,只需要看到pom. ...

  6. 原创:Spring整合junit测试框架(简易教程 基于myeclipse,不需要麻烦的导包)

    我用的是myeclipse 10,之前一直想要用junit来测试含有spring注解或动态注入的类方法,可是由于在网上找的相关的jar文件进行测试,老是报这样那样的错误,今天无意中发现myeclips ...

  7. SpringBoot整合ElasticSearch测试代码

    1.SpringBoot环境和测试类,我就不讲了. 2.yml中的配置 clustername:是es的集群名称,默认是elasticsearch cluster-nodes:tcp的地址,不要写错. ...

  8. SpringBoot 整合 Socket 实战案例 ,实现 单点发送、广播群发,1对1,1对多

    点击关注公众号,实用技术文章及时了解 作者:小目标青年 来源:https://blog.csdn.net/qq_35387940 本篇内容: 后端 + 前端简单HTML页面 功能场景点: 群发,所有人 ...

  9. SpringBoot整合Swagger测试api构建

    @Author:SimpleWu 什么是Swagger? Swagger是什么:THE WORLD'S MOST POPULAR API TOOLING 根据官网的介绍: Swagger Inspec ...

最新文章

  1. 隐私全无!错发1700多条Alexa录音,上报后亚马逊淡定回应是“个人错误”
  2. Unity镜子效果的实现(无需镜子Shader)
  3. Java面试题:给定一个整数,按10进制来看,计算里面包含多少个0
  4. php 7.1/7.3使用 json_encode 函数造成浮点类型数据出现精度问题
  5. 5G为何采纳华为力挺的Polar码?一个通信工程师的大实话
  6. socket与TCP/UDP编程-转
  7. ajax嵌套ajax的坏处,promise解决ajax的多重嵌套
  8. 免费制作gif图片工具
  9. 四川省大学生计算机作品大赛,我校学子在2019“新华三杯”四川省大学生计算机作品大赛中获奖20项...
  10. PySide6开发-03-QLineEdit
  11. 创新创业孵化知识体系-创业实践导论之-技术专栏
  12. 单片机驱动DM9000
  13. PS cs6是哪一年发布的?
  14. Centos7 安装独立显卡驱动
  15. 财务会计 -- 什么是借贷
  16. Matlab用figure;subplot后合并子图
  17. 视频监控存储特点分析
  18. ios 按钮下面加下划线_如何在iOS按钮上的文字添加下划线
  19. 00后大学生,学微积,用手机,从味同嚼蜡到喜闻乐见
  20. mysql安装简书_MySQL 安装

热门文章

  1. 互联网七字诀:专注、极致、口碑、快(雷总提出)
  2. Windows小技巧12--永久关闭开机弹出的今日热点
  3. 万万没想到系列,世界上最知名的失败建筑设计合集!
  4. 科目一 扣分 罚款 记住的内容
  5. Mac OS 显示隐藏文件
  6. IPv6地址及其报文----1
  7. 天选打工人春节兼职记
  8. 三维重建中旋转矩阵与平移矩阵思想误区
  9. 系统学习javaweb-13-SpringMVC+Hibernate
  10. 微型计算机怎么拆开装硬盘,硬盘怎么拆下来图解