前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用DbUnit,可以用Ant的任务来实现,也可以直接写DbUnit的测试代码实现 AntMySQLMobilejunitJDBC

使用DbUnit,可以用Ant的任务来实现,也可以直接写DbUnit的测试代码实现 AntMySQLMobilejunitJDBC

作者头像
阿敏总司令
发布2019-02-27 16:13:11
5990
发布2019-02-27 16:13:11
举报
文章被收录于专栏:简单就是美!简单就是美!

阅读更多

Ant: http://ant.apache.org/

Apache Ant is a Java-based build tool. In theory, it is kind of like Make, but without Make's wrinkles.

DbUnit:http://www.dbunit.org/

DbUnit is a JUnit extension (also usable with Ant) targeted for database-driven projects that, among other things, puts your database into a known state between test runs. This is an excellent way to avoid the myriad of problems that can occur when one test case corrupts the database and causes subsequent tests to fail or exacerbate the damage.

DbUnit has the ability to export and import your database data to and from XML datasets. Since version 2.0, DbUnit can works with very large dataset when use in streaming mode. DbUnit can also helps you to verify that your database data match expected set of values.

JUnitDoclet:http://www.junitdoclet.org/

Why wasn't this tested before? This question often arises shortly before a deadline. Our answer: Because testing was not easy enough.

Especially creating and maintaining a test structure is often used as an excuse. JUnitDoclet lowers the step toward JUnit. It generates skeletons of TestCases based on your application source code. And it supports you to reorganize tests during refactoring. Suddenly all the excuses don't count any longer.

MySQL数据库:Pagination,表:Persioninfo信息如下:

# Host: localhost # Database: pagination # Table: 'personinfo' # CREATE TABLE `personinfo` (   `id` varchar(100) NOT NULL default '',   `name` varchar(50) NOT NULL default '',   `sex` tinyint(1) NOT NULL default '0',   `mobile` varchar(50) NOT NULL default '0',   `address` varchar(50) NOT NULL default '',   `memo` varchar(50) default '',   PRIMARY KEY  (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=gb2312;

一、用Ant的任务来实现

  1、DataSelt.xml内容如下:

<!----><dataset><!----><?xml version="1.0" encoding="GBK"?> <dataset>  <Personinfo id="9001" name="YuLimin" sex="0" mobile="13800138000" address="中国福建莆田" memo="Ant DBUnit Test"/>  <Personinfo id="9002" name="俞Limin" sex="1" mobile="13800138000" address="中华人民共和国" memo="Ant DBUnit Test"/>  <Personinfo id="9003" name="俞黎敏" sex="0" mobile="13800138000" address="中华人民共和国福建" memo="Ant DBUnit Test"/> </dataset>

  2、Ant 的 build.xml内容如下,请进行相应的改动:

<!----><project name="DbUnitTest" default="DbUnit" basedir="."><!----><?xml version="1.0" encoding="GBK"?> <project name="DbUnitTest" basedir="." default="initdb">  <!-- 定义DbUnit需要的包,包括DbUnit包和jdbc包。 -->  <property name="lib.dir" value="E:\OpenSource"/>  <property name="dbunit.jar" value="${lib.dir}\DBUnit\dbunit-2.1.jar"/>  <property name="jdbc.jar" value="${lib.dir}\MySQL\lib\mysql-connector-java-3.1.7-bin.jar"/>  <property name="junit.jar" value="${lib.dir}\JUnit\junit.jar"/>  <property name="junitdoclet.jar" value="${lib.dir}\JUnitDoclet\JUnitDoclet.jar"/>  <property name="classes.dir" value="./classes"/>  <!-- 在全局属性定义中定义数据库连接的url,driver,userid,password,进行多个操作可以达到重用 -->  <property name="dburl" value="jdbc:mysql://localhost/Pagination"/>  <property name="dbdriver" value="com.mysql.jdbc.Driver"/>  <property name="dbuserid" value="root"/>  <property name="dbpwd" value="password"/>  <path id="DbUnit.classpath">   <pathelement location="${dbunit.jar}"/>   <pathelement location="${jdbc.jar}"/>   <pathelement location="${junit.jar}"/>   <pathelement location="${junitdoclet.jar}"/>  </path>  <target name="init">   <!-- 定义DbUnit的Ant任务类 -->   <taskdef name="dbunit" classname="org.dbunit.ant.DbUnitTask" classpathref="DbUnit.classpath"/>  </target>  <target name="initdb" depends="init">   <dbunit driver="${dbdriver}" supportBatchStatement="false" url="${dburl}" userid="${dbuserid}" password="${dbpwd}" classpathref="DbUnit.classpath">    <!-- operation的类型有:UPDATE 、INSERT、DELETE 、DELETE_ALL 、TRUNCATE 、REFRESH(MSSQL_REFRESH)、CLEAN_INSERT(MSSQL_CLEAN_INSERT)、NONE -->    <!-- (注意:MSSQLServer中在CLEAN_INSERT和REFRESH中要使用后面的MSSQL_REFRESH和MSSQL_CLEAN_INSERT) -->    <!-- INSERNT仅插入数据 -->    <!-- CLEAN_INSERT清除并插入数据 -->    <operation type="INSERT" src="DataSet.xml"/>   </dbunit>  </target>  <target name="export" depends="init">   <!-- 导出全部包含有数据的表的数据出来 -->   <dbunit driver="${dbdriver}" supportBatchStatement="false" url="${dburl}" userid="${dbuserid}" password="${dbpwd}" classpathref="DbUnit.classpath">    <export dest="Export.xml"/>   </dbunit>  </target>  <target name="select" depends="init">   <!-- 导出一个由查询语句产生的数据集和指定的一个表的数据 -->   <dbunit driver="${dbdriver}" supportBatchStatement="false" url="${dburl}" userid="${dbuserid}" password="${dbpwd}" classpathref="DbUnit.classpath">    <export dest="Select.xml">     <query name="Personinfo" sql="Select * From Personinfo Where name='YuLimin'"/>     <table name="Personinfo"/>    </export>   </dbunit>  </target> </project>

3、运行:ant或ant initdb或ant export或ant select不同的任务

二、直接写DbUnit的测试代码实现,DbUnitTest.java的代码如下:

import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager;

import org.dbunit.DatabaseTestCase; import org.dbunit.database.DatabaseConnection; import org.dbunit.database.IDatabaseConnection; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.xml.FlatXmlDataSet; import org.dbunit.operation.DatabaseOperation;

/**  *

Title: 运行DbUnit测试将数据导入数据库。

 *

 *

Description: 运行DbUnit测试将数据导入数据库。

 *

 *

Copyright: Copyright (c) 2004

 *

 *

Company: Beyond DayBreak Office

 * @author YuLimin

 * @version 1.0

 */

public class DbUnitTest extends DatabaseTestCase

{

    public DbUnitTest(String name)

    {

        super(name);

    }

    /**      *      * @see org.dbunit.DatabaseTestCase#getConnection()      * @throws Exception      * @return IDatabaseConnection      */     protected IDatabaseConnection getConnection() throws Exception     {         Class driverClass = Class.forName("com.mysql.jdbc.Driver");         Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost/Pagination","root","password");         return new DatabaseConnection(jdbcConnection);     }

    /**      *      * @see org.dbunit.DatabaseTestCase#getDataSet()      * @throws Exception      * @return IDataSet      */     protected IDataSet getDataSet() throws Exception     {         return new FlatXmlDataSet(new FileInputStream("DataSet.xml"));     }

    /**      * getSetUpOperation      *      * @return DatabaseOperation      * @throws Exception      */     protected DatabaseOperation getSetUpOperation() throws Exception     {         return DatabaseOperation.REFRESH;     }

    /**      * getTearDownOperation      *      * @return DatabaseOperation      * @throws Exception      */     protected DatabaseOperation getTearDownOperation() throws Exception     {         return DatabaseOperation.NONE;     }

    /**      * 运行DbUnit测试将数据导入数据库。      */     public void testDbUnit()     {         System.out.println("DbUnit Testing...");     } }

(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('<div id="' + s + '"></div>'); (window.slotbydup=window.slotbydup || []).push({ id: '4774193', container: s, size: '728,90', display: 'inlay-fix' }); })(); jQuery(function(){ window.csdn.recommendSide({ contentBox: "recommend", postBox: jQuery(".news-right-side"), query:'使用DbUnit,可以用Ant的任务来实现,也可以直接写DbUnit的测试代码实现', popu:'725' }) });

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2005-04-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档