テストクラス修正(S2TestCase対応)
テストの際にDBをイニシャライズするのにはDB2Excelという便利なものが用意されている。
これは読んで字の如くDBとExcelデータのやりとりをしてくれるスグレモノのようだ。
早速組みこんでみよう。
組み込むにあたって以下の変更を実行
- コンテナをBelongToDaoTestクラスで作成していたが、S2TestCaseで自動作成してくれるのでその対応
- 初期データをExcelファイルから読み込むようにしかける
- S2TestCaseでトランザクション制御をやってくれるらしいので各テストメソッド名の末尾に"Tx"を追記
以上を組み込んだ結果(BelongToDaoTest.java)↓
public class BelongToDaoTest extends S2TestCase { private static BelongToDao BELONGTO_DAO = null; protected void setUp() throws Exception { //DAOのダイコンファイルを読み込んでおく include("redrisefirm/seasar/s2dao/dao.dicon"); //DAOを取得する BELONGTO_DAO = (BelongToDao) getComponent(BelongToDao.class); } public final void testGetBelongToEntityOneTx() { //初期データを読み込む readXlsWriteDb("redrisefirm/seasar/s2dao/util/getBelongToResult.xls"); BelongToEntity belongToEntity = BELONGTO_DAO.getBelongToEntity(1); //答え合わせ assertEquals("値が違います", 1, belongToEntity.getBelongToCode()); assertEquals("値が違います", "地球連邦", belongToEntity.getBelongToName()); } public final void testGetBelongToEntityTwoTx() { //初期データを読み込む readXlsWriteDb("redrisefirm/seasar/s2dao/util/getBelongToResult.xls"); //Entityの取得 BelongToEntity belongToEntity = BELONGTO_DAO.getBelongToEntity(2); //答え合わせ assertEquals("値が違います", 2, belongToEntity.getBelongToCode()); assertEquals("値が違います", "ジオン公国", belongToEntity.getBelongToName()); } public final void testGetBelongToEntityNoNumberTx() { //初期データを読み込む readXlsWriteDb("redrisefirm/seasar/s2dao/util/getBelongToResult.xls"); //Entityの取得 BelongToEntity belongToEntity = BELONGTO_DAO.getBelongToEntity(-1); //答え合わせ assertNull(belongToEntity); } public final void testInsertTx() { //初期データを読み込む readXlsWriteDb("redrisefirm/seasar/s2dao/util/getBelongToResult.xls"); //insert用のentityを作成 BelongToEntity belongToEntity = new BelongToEntity(); belongToEntity.setBelongToCode(99); belongToEntity.setBelongToName("デラーズフリート"); //実行 BELONGTO_DAO.insert(belongToEntity); //答え合わせ BelongToEntity answerEntity = BELONGTO_DAO.getBelongToEntity(99); assertTrue("値が違います", belongToEntity.equals(answerEntity)); } }
これで実行したらオールグリーン。しかもちゃんとトランザクションがかかっていた。
うまくいったところで今日はこの辺で。