spring boot应用测试框架介绍
一、spring boot应用测试存在的问题
官方提供的测试框架spring-boot-test-starter,虽然提供了很多功能(junit、spring test、assertj、hamcrest、mockito、jsonassert、jsonpath),Spring、Spring Boot和TestNG测试指南 - 集成测试中用Docker创建数据库但是在数据库层面,依旧存在问题,它强烈依赖于数据库中的数据,并且自身不具备数据初始化的能力。测试框架spring-test-dbunit与spring-boot-unitils-starter支持spring-boot应用的测试,同时,也提供单元测试前置数据准备的功能。
二、spring-test-dbunit介绍与应用
2.1、介绍
spring-test-dbunit是spring boot的作者之一Phillip Webb开发的、用于给spring项目的单元测试提供dbunit功能的开源项目。dbunit项目的介绍为:puts your database into a known state between test runs。spring-test-dbunit的官网介绍为:Spring DBUnit provides integration between the Spring testing framework and the popular DBUnit project。
2.2、应用
实例主要代码如下:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoTestApplication.class)
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
TransactionDbUnitTestExecutionListener.class})
@DbUnitConfiguration(dataSetLoader = XlsDataSetLoader.class)
@Transactional
public class UserControllerTest {
@Autowired
private UserController userController;
@Test
@DatabaseSetup({"/data/test_getUsername.xls"})
public void test_getUsername() {
String username = userController.getUsername(1234);
Assert.assertTrue(username.equals("zhangsan"));
}
}
test_getUsername.xls的内容如下:
2.3、实现原理
测试环境准备:
@RunWith(SpringJUnit4ClassRunner.class):用于启动测试、注册TestExecutionListener、构建testContext。
@SpringBootTest(classes = DemoTestApplication.class):利用SpringBootTestContextBootstrapper加载applicationContext。
DependencyInjectionTestExecutionListener:用来将bean注入到测试的class中,例如实例中的userController。
事务实现原理:
这里的TransactionalTestExecutionListener简称T,DbUnitTestExecutionListener简称D,如下图:
运行流程为:初始化测试类-->开始事务-->准备测试数据-->运行测试方法-->进行expectedData校验-->回滚或者提交事务。这就保证了整个方法的测试过程中,数据准备、测试方法运行、测试数据校验都在一个事务里面,最后事务如果回滚了,就不会在测试数据库中留下测试数据。
三、spring-boot-unitils-starter介绍与应用
3.1、介绍
unitils框架介绍:Unitils is an open source library aimed at making unit and integration testing easy and maintainable。堪称测试之王,其组成结构如下:
unitils目前只支持xml配置的spring项目,对于spring-boot项目稍不支持,基于此,我就开源一个项目,用于在unitils和spring-boot应用之间建立起桥梁。
这个开源项目主要做了以下工作:
- 重写SpringModule->SpringBootModule,支持ApplicationContext的设置
- ApplicationContext设置到SpringBootModule中
- DataSource替换
- 支持xls的dataSet
目前可用的版本为:
<dependency>
<groupId>com.github.yangjianzhou</groupId>
<artifactId>spring-boot-unitils-starter</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
3.2、应用
实例代码如下:
@RunWith(UnitilsBootBlockJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoTestApplication.class)
@Transactional(value = TransactionMode.ROLLBACK)
public class UserControllerTest {
@SpringBeanByType
private UserController userController;
@Test
@DataSet({"/data/test_getUsername.xls"})
public void test_getUsername() {
String username = userController.getUsername(1234);
Assert.assertTrue(username.equals("zhangsan"));
}
}
3.3、实现原理
DatabaseModule下的DatabaseTestListener进行了事务的开启与回滚(提交)。
DbUnitModule下的DbUnitListener进行了dataset的准备与expecteddataset的校验。
SpringBootModule下的SpringTestListener进行了测试类中属性的注入与销毁测试类。
四、扩展
spring-test-dbunit(http://springtestdbunit.github.io/spring-test-dbunit/)与spring-boot-unitils-starter(https://github.com/yangjianzhou/spring-boot-unitils)弥补了spring-boot-test-starter在数据库测试方面的不足,结合框架spring-test-dbunit(或者spring-boot-unitils-starter)与mock工具(mockito)以及一些测试方法,可以很好的完成单元测试。
但是,spring-test-dbunit与spring-boot-unitils-starter各有优缺点,spring-test-dbunit有良好的文档,但是最近更新版本为2016年版,仅仅是数据库层面的测试工具。spring-boot-unitils-starter利用了unitils的优势,可以说是一个测试平台了,虽然说,每年都在发布版本(unitils),但是其文档较少。用户可以根据自己的需要进行选择。
附:文中涉及到的测试样例代码:https://github.com/yangjianzhou/spring-boot-test-sample
原文链接:https://my.oschina.net/yangjianzhou/blog/1859103
spring boot应用测试框架介绍的更多相关文章
- Laravel 和 Spring Boot 两个框架比较创业篇(二:人工成本)
前面从开发效率比较了 Laravel 和 Spring Boot两个框架,见:Laravel 和 Spring Boot 两个框架比较创业篇(一:开发效率) ,这一篇打算比较一下人工成本. 本文说的人 ...
- Spring Boot(十二):spring boot如何测试打包部署
Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...
- 【spring boot】8.spring boot的日志框架logback使用
在继续上一篇的Debug调试之后,把spring boot的日志框架使用情况逐步蚕食. 参考:http://tengj.top/2017/04/05/springbo 开篇之前,贴上完整applica ...
- Spring Boot从入门到放弃-Spring Boot 整合测试
站长资讯摘要:使用Spring Boot 整合测试,对Controller 中某个方法进行测试或者对Service,Mapper等进行测试,不需要运行项目即可查看运行结果是否和期望值相同,Spring ...
- Java框架Spring Boot & 服务治理框架Dubbo & 应用容器引擎Docker 实现微服务发布
微服务系统架构实践 开发语言Java 8 框架使用Spring boot 服务治理框架Dubbo 容器部署Docker 持续集成Gitlab CI 持续部署Piplin 注册中心Zookeeper 服 ...
- Spring boot maven 搭建框架
Spring Boot:目的:这个框架帮助开发者更容易地创建基于Spring的应用程序和服务,使得pring开发者能够最快速地获得所需要的Spring功能.优点:完全不需要XML配置,让spring应 ...
- Laravel 和 Spring Boot 两个框架比较创业篇(一:开发效率)
我个人是比较不喜欢去正儿八经的比较两个框架的,这样没有意义,不过欲善其事先利其器! 技术是相通的,但是在某个特定的领域的某个阶段肯定有相对最适合的一个工具! 这里比较不是从技术角度比较,而是从公司技术 ...
- (转)Spring Boot(十二):Spring Boot 如何测试打包部署
http://www.ityouknow.com/springboot/2017/05/09/spring-boot-deploy.html 有很多网友会时不时的问我, Spring Boot 项目如 ...
- Spring Boot(十二):Spring Boot 如何测试打包部署
有很多网友会时不时的问我, Spring Boot 项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下 Spring Boot 如何开发.调试.打包到最后的投产上线. 开发阶段 ...
随机推荐
- (转)Unity笔记之编辑器(Foldout、HelpBox、InspectorTitlebar、Slider、MinMaxSlid ...
1. Foldout.HelpBox 折叠菜单,大家都知道,不具体解释了,直接代码.因为折叠菜单中必然是有内容才能看到效果,所以顺带把HelpBox(提示框)也说了. [code]csharpcode ...
- WPF实用知识点
1.一个基本的WPF程序, 需要引入的程序集WindowsBase, PresentationCore, PresentationFramework using System; using Syste ...
- 在ASP.NET MVC3 中利用JSONP跨域登录WEB系统
在信息系统开发的时,根据相关业务逻辑难免会多系统之间互相登录.一般情况下我们需要在多系统之间使用多个用户名和密码.这样客户就需要在多个系统之间重复登陆.每次登录都需要输入用户名和密码.最近比较流行的就 ...
- MVC结构简介
本文编译自J2EE的相关文档.MVC(Model-View-Controller)应用程序结构被用来分析分布式应用程序的特征.这种抽象结构能有助于将应用程序分割成若干逻辑部件,使程序设计变得更加容易. ...
- sql的case when then else end 的语法实现列转行
SELECT * FROM test5 ; RESOURCES DATETIME CNT ID1 0 2018-01-22 4 12 0 2018-01-24 10 23 0 2018-01-25 2 ...
- ZOJ 3941 Kpop Music Party(省赛, 贪心)
Kpop Music Party Time Limit: 2 Seconds Memory Limit: 65536 KB Marjar University often hosts Kpo ...
- Learning PHP Design Patterns
Learning PHP Design Patterns CHAPTER 1 Algorithms handle speed of operations, and design patterns ha ...
- Docker Libnetwork driver API
以下内容均在libnetwork/driverapi目录下 Driver接口如下所示: // Driver is an interface that every plugin driver needs ...
- window7安装MongoDB详细步骤
1.下载安装包 下载地址:https://www.mongodb.com/download-center/community 2.鼠标右击安装包,进行安装 3.选自定义安装 4.千万不要勾选 5.打开 ...
- .tomcat 管理 给tomcat设置用户名和密码登录
如果点击红色框框 出现403以下错误 看这个帖子可以解决 tomcat访问管理页面出现:403 Access Denied 解决方法 tomca管理 测试功能,生产环境不要用. Tomcat管理功能用 ...