专栏首页简单就是美!使用DbUnit,可以用Ant的任务来实现,也可以直接写DbUnit的测试代码实现 AntMySQLMobilejunitJDBC

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

阅读更多

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' }) });

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 关于WAS当中FFDC报告error in opening zip file剖析及解决 博客分类: Java Strutslog4jIBM

    关于WAS当中FFDC报告java.util.zip.ZipException: error in opening zip file剖析及解决

    阿敏总司令
  • 关于Tomcat 5.5.XX数据源配置出现连接问题的解决

    关于Tomcat 5.5.XX数据源配置出现连接问题的解决 错误信息:Cannot create JDBC driver of class '' for con...

    阿敏总司令
  • JavaScript 解析 XML 时如何通过名称直接取到值?

    同级的Attribute,有没有办法通过类似于 getXXXByName("aa") 得到0, getXXXByName("bb") 得到1呢?

    阿敏总司令
  • Springboot读取配置文件、pom文件及自定义配置文件

    前言 很多人都知道读取配置文件,这是初级做法,上升一点难度是使用java bean的方式读取自定义配置文件,但是大家很少有知道读取pom文件信息,接下来我都会讲...

    胖虎
  • python用户输入(三)

    name = "Li QW" #定义变量 fname = name  #fname等于name变量产生的值 print("My name is " , name...

    py3study
  • 原生js实现on和emit

    转载自:http://www.cnblogs.com/aisiqi-love/p/10594461.html

    smy
  • gitbook 入门教程之超高颜值的思维导图simple-mind-map插件

    在您的 gitbook 的 book.json 文件中,将 simple-mind-map 添加到 plugins 列表中.

    雪之梦技术驿站
  • 你用 Python 写过哪些牛逼的程序/脚本?

    【导读】:有网友在 Quora 上提问,「你用 Python 写过最牛逼的程序/脚本是什么?」。本文摘编了 3 个国外程序员的多个小项目,含代码。 Manoj...

    小小科
  • 不会框架不要紧,我带你自定义框架

    前言:这标题说的有点大了,当一回标题党,之前在学JSP的时候提到了JSTL和EL表达式,由于一直钟情于Servlet,迟迟没有更新别的,这回算是跳出来了。这回放...

    泰斗贤若如
  • 你用 Python 写过哪些牛逼的程序/脚本?

    【导读】:有网友在 Quora 上提问,「你用 Python 写过最牛逼的程序/脚本是什么?」。本文摘编了 3 个国外程序员的多个小项目,含代码。

    小小科

扫码关注云+社区

领取腾讯云代金券