DBUnitでDaoの単体テスト

DBUnitでDao系のテストを行ってみました。
今回はxmlファイルで期待値や事前にDBに投入するデータを作りました。
(エクセルでもできるらしいけどやってません。)
DBUnitにかますxmlは以下のようなフォーマット。
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<tablename
column1="00000000001"
column2="ユニットテスト"/>
</dataset>

以下、テストケースのサンプル。
※以下のテストケースをそのまま使用するとエラーになります。
コメントに従ってエラーを修正すること。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.net.URL;

import junit.framework.TestCase;

import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.operation.DatabaseOperation;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.dataset.DataSetException;
import org.dbunit.Assertion;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.filter.DefaultColumnFilter;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HogeDaoTest extends TestCase {

HogeDaoDao dao = new HogeDaoDao();

/**
* 各テストメソッドの事前処理
* 本メソッドでテストで使用するDBのバックアップを行うこと
*
* @see junit.framework.TestCase#setUp()
*/
@Before
public void setUp() {

}

/**
* 各テストメソッドの事後処理
* 本メソッドでsetUpメソッドでバックアップしたファイルから
* テストで使用するDBのリストアを行うこと
*
* @see junit.framework.TestCase#tearDown()
*/
@After
public void tearDown() {

}

/**
* DBUnitのテストケースサンプル
*/
@Test
public void test_FindSample() {

// 準備データをDBに入れる
// prepareDataSetの引数にクラスパスの最上位からのファイルパスを文字列で設定する
prepareDataSet();

// Daoの参照系メソッドを呼び出す
List hogeDtoList = dao.find();

// 普通のテストケース同様に期待値と実際の値で比較する
assertEquals(1, hogeDtoList.size());
}

/**
* DBUnitのテストケースサンプル
*/
public void test_InsertSample() {

// Daoの挿入系メソッドを呼び出す
int insertRecNum = dao.insert();
try {

// DBから実際のデータの取得
// getDBDataにデータを取得したいテーブル名称を渡す
ITable actualTable = getDBData();

// 期待値データの取得
// getExpectedDataSetの引数にクラスパスの最上位からのファイルパスを文字列で設定する
IDataSet expectedDataSet = getExpectedDataSet();
// expectedDataSet.getTable に取得したい期待値のテーブル名称を渡す
ITable expectedTable = expectedDataSet.getTable();

// 比較できないカラムを除外する
// 比較できないカラムがない場合は以下2行は不要です
ITable filteredActualTable = excludeColumn(actualTable, new String[] {"upd_timestamp"});
ITable filteredExpectedTable = excludeUpdTimestamp(expectedTable, new String[] {"upd_timestamp"});

// 期待値と実際のデータの比較
Assertion.assertEquals(filteredExpectedTable, filteredActualTable);
assertEquals(1, insertRecNum);

} catch (DatabaseUnitException e) {

fail();
e.printStackTrace();
}
}

/**
* 比較対象外にしたい列名をフィルタリングする
* TIMESTAMP型の列に現在時刻を設定する場合などに
* その列をテスト結果の比較対象外としたい等々の場合
* に使用する
*
* @param actualTable
* ITable
* @param excludeColumnNames
* 除外したいカラム名称配列
*
* @return ITable
*/
protected ITable excludeColumn(ITable iTable, String[] excludeColumnNames) {

try {

return DefaultColumnFilter.excludedColumnsTable(iTable,
excludeColumnNames);

} catch (DataSetException e) {

e.printStackTrace();
throw new RuntimeException(e);
}
}

/**
* 予測データセットの取得
*
* @param extectedFilename
* 予測データセットファイル名称
* @return IDataSet
*/
protected IDataSet getExpectedDataSet(String extectedFilename) {

try {

return new FlatXmlDataSet(new FileInputStream(
getFilePath(extectedFilename)));

} catch (DataSetException e) {

e.printStackTrace();
throw new RuntimeException(e);

} catch (FileNotFoundException e) {

e.printStackTrace();
throw new RuntimeException(e);

} catch (IOException e) {

e.printStackTrace();
throw new RuntimeException(e);
}
}

/**
* tableNameのデータを取得
*
* @param tableName
* 取得対象テーブル名称
* @return ITable
*/
protected ITable getDBData(String tableName) {

IDatabaseConnection con = null;
try {

con = getConnection();
IDataSet databaseDataSet = con.createDataSet();
return databaseDataSet.getTable(tableName);

} catch (SQLException e) {

e.printStackTrace();
throw new RuntimeException(e);

} catch (DataSetException e) {

e.printStackTrace();
throw new RuntimeException(e);

} finally {

close(con);
}
}

/**
* DBコネクション取得
*
* @return IDatabaseConnection
*/
public IDatabaseConnection getConnection() {

try {

// DatabaseConnection の引数に
// java.sql.Connection
// を渡すこと。
return new DatabaseConnection();

} catch (Exception e) {

e.printStackTrace();
throw new RuntimeException(e);
}
}

/**
* fileの絶対パスを取得
*/
public String getFilePath(String file) {

URL url = Thread.currentThread().getContextClassLoader().getResource(
file);
return url.getFile();
}

/**
* テスト前のデータセットをDBに投入
*
* @param filename
* クラスパスの最上位からの相対パス
*/
public void prepareDataSet(String filename) {

IDatabaseConnection con = null;
try {

con = getConnection();
DatabaseOperation.CLEAN_INSERT.execute(con, new FlatXmlDataSet(
new FileInputStream(getFilePath(filename))));

} catch (DataSetException e) {

e.printStackTrace();
throw new RuntimeException(e);

} catch (FileNotFoundException e) {

e.printStackTrace();
throw new RuntimeException(e);

} catch (DatabaseUnitException e) {

e.printStackTrace();
throw new RuntimeException(e);

} catch (SQLException e) {

e.printStackTrace();
throw new RuntimeException(e);

} catch (IOException e) {

e.printStackTrace();
throw new RuntimeException(e);

} finally {

close(con);
}
}
}

2 コメント:

匿名

2008年8月20日 18:49  

ITable filteredExpectedTable = excludeUpdTimestamp(expectedTable, new String[] {"upd_timestamp"});



ITable filteredExpectedTable = excludeColumn(expectedTable, new String[] {"upd_timestamp"});

はずです

師子乃

2019年5月8日 20:32  

こんばんは。

DBのテストは事前、事後が大事ですね!