本文介绍了spring boot 与dbunit 配合使用方法,分享给大家,具体如下:
dbunit
快速上手
springboot 添加 dbunit 依赖
1 2 |
// https://mvnrepository.com/artifact/org.dbunit/dbunit testcompile group: 'org.dbunit' , name: 'dbunit' , version: '2.5.4' |
编写test.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import org.dbunit.dbtestcase; import org.dbunit.databaseunitexception; import org.dbunit.database.databaseconnection; import org.dbunit.database.idatabaseconnection; import org.dbunit.database.querydataset; import org.dbunit.dataset.datasetexception; import org.dbunit.dataset.idataset; import org.dbunit.dataset.xml.flatxmldataset; import org.dbunit.dataset.xml.flatxmldatasetbuilder; import org.dbunit.operation.databaseoperation;
@runwith (springrunner. class ) @springboottest public class dbunit extends dbtestcase {
@resource datasource datasource; idatabaseconnection idatabaseconnection;
@override protected idataset getdataset() throws exception { return idatabaseconnection.createdataset(); }
@before public void before() throws exception{ idatabaseconnection = new databaseconnection(datasource.getconnection());
} } |
将数据库数据转换为flatxml
1 2 3 4 5 6 |
@test public void testpartialexport() throws datasetexception, ioexception { querydataset querydataset = new querydataset(idatabaseconnection); querydataset.addtable( "user" , "select * from user" ); flatxmldataset.write(querydataset, new fileoutputstream( "user.xml" )); } |
执行后,将会得到一个user.xml文件,里面记录了数据库user表的所有数据,看起来大概是这个样子
1 2 3 4 5 6 |
<?xml version= '1.0' encoding= 'utf-8' ?> <dataset> <user id= "1" username= "mechanists" password= "aba3fc1eb2997e318e43ca099ae175ca" /> <user id= "2" username= "reporter" password= "aba3fc1eb2997e318e43ca099ae175ca" />
</dataset> |
idataset
官网文档地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://segmentfault.com/a/1190000016337648
查看更多关于Spring Boot 与DBunit 配合使用方法的详细内容...