Spring2.5とiBatisでDAOを作成

試しにspring2.5バージョンでDAOを作成しました。
まずはインサートのみ。

  • DAOインタフェース

package jp.eshopstreet.domain.area.dao;

import jp.eshopstreet.domain.area.model.LocalAreaDefineDto;

public interface LocalAreaDefineDao {

    void insert(LocalAreaDefineDto model);
}

  • DAOの実装

package jp.eshopstreet.domain.area.dao.impl;

import jp.eshopstreet.domain.area.dao.LocalAreaDefineDao;
import jp.eshopstreet.domain.area.model.LocalAreaDefineDto;

import org.epitaph.framework.ibatis.BaseSqlMapClientDao;
import org.springframework.stereotype.Repository;

@Repository
public class LocalAreaDefineDaoImpl extends BaseSqlMapClientDao implements LocalAreaDefineDao {

    @Override
    public void insert(LocalAreaDefineDto model) {
        super.getSqlMapClientTemplate().insert("LocalAreaDefine.insert", model);
    }
}

Repositoryアノテーションは、前回説明した通り。
コンテナへ登録してくれて例外を自動的に変換してくれます。

問題は、JUnit単体テストをどうやるか。以下予定。

    • 基本的にテストデータはEXCELで突っ込んで、テストが終わったらロールバックする。
    • アノテーションが充実したJUnit4で実行。
    • 基本的にはテスト対象クラスを自動インジェクション。
    • サービスクラスなどは呼び出し先のDAOやサービスはスタブ化

spring2.5の英語ドキュメントによるとSpring TestContextフレームワークで色々とできるようです。

@ConfigurationContextはかなり使えそう。
テストクラスの基底クラスでトランザクションなどを定義したベースのxmlファイルを読み込ませて
各テストクラス作成者は追加で拡張したxmlを読み込ませる事ができます。

  • ベースのテストクラス

@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from "/base-context.xml" in the root of the classpath
@ContextConfiguration(locations={"/base-context.xml"})
public class BaseTest {
// class body...
}
// ApplicationContext will be loaded from "/base-context.xml" and "/extended-context.xml"

  • 拡張したテストクラス

// in the root of the classpath
@ContextConfiguration(locations={"/extended-context.xml"})
public class ExtendedTest extends BaseTest {
// class body...
}

超便利、っていうか超エッチ・・・

こんな感じでテストクラス作成。(※Excel対応とかはまだ)

package jp.eshopstreet.domain.area.dao.impl;

import java.sql.Timestamp;

import jp.eshopstreet.domain.area.dao.LocalAreaDefineDao;
import jp.eshopstreet.domain.area.model.LocalAreaDefineDto;

import org.epitaph.framework.unit.DaoTestCase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/beanRefContext.xml"})
public class LocalAreaDefineDaoImplTest extends DaoTestCase {

    @Autowired
    private LocalAreaDefineDao localAreaDefineDao;

    @BeforeClass
    public static void beforeClass() {
        System.out.println("before class");
    }

    @Test
    public void insert(){
        LocalAreaDefineDto model = new LocalAreaDefineDto();
        localAreaDefineDao.insert(model);
        System.out.println("test");
    }

    @AfterClass
    public static void afterClass() {
        System.out.println("after class");
    }
}

とりあえず、呼び出しには成功。テストはしてません。

Excelでデータ突っ込んで、あとはテストが終わったらロールバックするようにしてみます。

dbUnitあたりと連携かな。