DbUnitを使ってデータベースの精査をできるようにした。
テスト対象コンポーネント
テスト対象コンポーネントは次の通り。
package sample.dicon.component;import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;import sample.dicon.constant.Result;
import sample.dicon.object.Address;
import sample.dicon.object.Return;
import sample.dicon.object.Sysdate;
import sample.dicon.object.impl.ReturnImpl;/**
* 住所録を更新する。
* (セッター・インジェクション)
*/
public class UpdateAddressBook_Type2 {private Connection conn = null;
private Sysdate sysdate = null;
private static QueryRunner qr = new QueryRunner();
public void setConnection(Connection conn) {
this.conn = conn;
}public void setSysdate(Sysdate sysdate) {
this.sysdate = sysdate;
}public Return execute(Address address) throws SQLException {
// 住所録を更新を更新。
Object[] params = { address.getName(), address.getAge(),
address.getSex().getValue(), address.getZipCode(),
address.getAddress(), address.getTel(),
sysdate.getNow().getTimestamp(), address.getId(),
address.getUpdateDate().getTimestamp() };
String update = "UPDATE ADDRESS_BOOK SET NAME=?, AGE=?, SEX=?, ZIP_CODE=?, ADDRESS=?, TEL=?, UPDATE_DATE=? WHERE ID=? AND UPDATE_DATE=?";
int count = qr.update(conn, update, params);// 更新件数が0件の場合は失敗を返す。
if (count < 1) {
// 住所録の変更をロールバック。
conn.rollback();// 失敗を返す。
return new ReturnImpl(Result.FAILURE, "更新に失敗しました。");
}// 住所録の変更をコミット。
conn.commit();// 成功を返す。
return new ReturnImpl(Result.SUCCESS, "更新に成功しました。");
}}
テストケースXML
テストケースのXMLファイルは次の通り。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dicontestcase SYSTEM "../../dtd/dicontestcase.dtd">
<dicontestcase>
<!--
テスト名を記述
※未定義の場合はファイル名(拡張子を除く)をテスト名とする
-->
<name>サンプルテスト その1</name>
<!-- テストの説明 -->
<description>
正常系 : 住所録を更新する。
</description>
<!-- テストするコンポーネント -->
<target>sample.dicon.component.UpdateAddressBook_Type2</target>
<!--<target type="3">sample.dicon.component.UpdateAddressBook_Type3</target>-->
<!-- 入力する値 -->
<input>
<!-- 実行するメソッド -->
<execute name="execute">
<arg type="sample.dicon.object.Address">
<bean type="sample.dicon.object.impl.AddressImpl">
<prop name="id" value="009"/>
<prop name="name" value="猪川千恵"/>
<prop name="age" value="25" type="java.lang.Integer"/>
<prop name="sex">
<const type="sample.dicon.constant.Sex" name="FEMALE"/>
</prop>
<prop name="zipCode" value="3339999"/>
<prop name="address" value="▲▲県■■市■■9−6−3"/>
<prop name="tel" value="00011112222"/>
<prop name="updateDate" value="2004-07-15 12:13:12" type="sample.dicon.util.Date"/>
<prop name="createDate" value="2004-06-10 20:10:30" type="sample.dicon.util.Date"/>
</bean>
</arg>
</execute>
<!-- コンポーネントの状態 -->
<component>
<inject name="connection" type="java.sql.Connection">
<bean type="sample.dicon.object.impl.ConnectionImpl">
<constructor>
<arg type="java.lang.String" value="jdbc:oracle:thin:@//localhost:1521/myoracle"/>
<arg type="java.lang.String" value="sugawara"/>
<arg type="java.lang.String" value="sugawara"/>
</constructor>
</bean>
</inject>
<inject name="sysdate" type="sample.dicon.object.Sysdate">
<bean type="sample.dicon.object.impl.SysdateImpl">
<constructor>
<arg type="java.lang.String" value="2004-10-20 12:24:30"/>
</constructor>
</bean>
</inject>
</component>
<!-- データベースの状態 -->
<database file="dataset/sample1-InputDataSet.xls"/>
<!--<database file="dataset/sample1-InputDataSet.xml"/>-->
</input>
<!-- 期待する値 -->
<expected>
<!-- メソッドの戻り値 -->
<return>
<bean type="sample.dicon.object.impl.ReturnImpl">
<constructor>
<arg type="sample.dicon.constant.Result">
<const type="sample.dicon.constant.Result" name="SUCCESS"/>
</arg>
<arg type="java.lang.String" value="更新に成功しました。"/>
</constructor>
</bean>
</return>
<!-- コンポーネントの状態 -->
<component>
<!-- connectionは精査しない -->
<inject name="sysdate" type="sample.dicon.object.Sysdate">
<bean type="sample.dicon.object.impl.SysdateImpl">
<constructor>
<arg type="java.lang.String" value="2004-10-20 12:24:30"/>
</constructor>
</bean>
</inject>
</component>
<!-- データベースの状態 -->
<database file="dataset/sample1-ExpectedDataSet.xls"/>
<!--<database file="dataset/sample1-ExpectedDataSet.xml"/>-->
</expected>
</dicontestcase>
テスト結果
XMLTestRunnerの実行結果は次の通り。

その他
サンプルを含むEclipsのプロジェクト一式はここ。
