dbunit介紹及使用

來源:互聯網
上載者:User
官方網址: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.

1. Dbunit無法識別mysql5的bit資料類型,所以需要編寫新的DataTypeFactory,對bit類型作處理。

package net.jim.database.dbunit;

import java.sql.Types;

import org.dbunit.dataset.datatype.DataType;
import org.dbunit.dataset.datatype.DataTypeException;
import org.dbunit.ext.mysql.MySqlDataTypeFactory;

public class MySql5DataTypeFactory extends MySqlDataTypeFactory {

    public DataType createDataType(int sqlType, String sqlTypeName)
            throws DataTypeException {
        if (sqlType == Types.OTHER) {
            // BOOLEAN
            if ("bit".equals(sqlTypeName)) {
                return DataType.BOOLEAN;
            }
        }

        return super.createDataType(sqlType, sqlTypeName);
    }
}

2. 擴充dbunit的DatabaseTestCase類,實現getConnection()和getDataSet()方法,這樣,在寫測試案例的時候只需要實現該抽象類別即可。

package net.jim.site.dbunit;

import java.io.FileInputStream;

import net.jim.database.dbunit.MySql5DataTypeFactory;
import net.jim.site.util.TestUtil;

import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.XmlDataSet;

public abstract class DatabaseTestCase extends org.dbunit.DatabaseTestCase {

    @Override
    protected IDatabaseConnection getConnection() throws Exception {
        IDatabaseConnection connection = new DatabaseConnection(TestUtil
                .getConnection());
        DatabaseConfig config = connection.getConfig();
        config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
                new MySql5DataTypeFactory());
        return connection;
    }

    @Override
    protected IDataSet getDataSet() throws Exception {
        return new XmlDataSet(new FileInputStream(
                "src/test/net/jim/site/dbunit/SiteData.xml"));
    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.