SpringでMavenなプロジェクトでDBUnitを使ってテスト

■ データのバックアップ DBUnitの機能でAntでデータのバックアップをXMLに取ってくれるのがあったので、 http://muimi.com/j/test/dbunit/を参考にやってみました。   今回はMavenなプロジェクトなのでmaven-antrun(http://maven.apache.org/plugins/maven-antrun-plugin/)を 使ってpom.xmlの中からAntタスクを叩きました。 #Maven上でクラスパス通せるのがナイス

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
        <executions>
        <execution>
            <id>export</id>
            <phase>process-test-resources</phase>
            <configuration>
                <target name="export">
                    <property name="test_classpath" refid="maven.test.classpath"/>
                    <echo message="test classpath:    ${test_classpath}"/>
                    <taskdef name="dbunit" classname="org.dbunit.ant.DbUnitTask" classpathref="maven.test.classpath" />
                    <dbunit driver="com.mysql.jdbc.Driver"
                        url="jdbc:mysql://localhost/spring"
                        userid="root"
                        password="">
                    <export dest="data/export.xml"/>
                    </dbunit>
                  </target>
               </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

phaseをどこにすべきかなんかちょっと微妙ですが、これでmvn test叩いたら所定のファイルに 以下のようにイイ感じに抜き出せました。これでガンガンDB系の自動テストを心置きなく、と。

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <item id="1" name="lemon" price="50" description="vitaminC!"/>
  <item id="2" name="apple" price="100" description="sweet!"/>
  <item id="3" name="orange" price="200" description="fresh!!"/>
  <team team_id="1" name="hoge"/>
</dataset>

    ■ テストクラス http://walbrix.net/blog/2010/05/spring-dbunit.htmlを参考してみます。   DataSourceBasedDBTestCaseというクラスをextendsして DBUnitが使う以下2つのメソッドをオーバーライドします。

@Autowired
protected DataSource dataSource;
~略~
@Before
public void setUp() {
    try {
        super.setUp();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
~略~
// for DBUnit
@Override
protected DataSource getDataSource() {
    return dataSource;
}
@Override
protected IDataSet getDataSet() throws Exception {
    return new CsvDataSet(new File("data"));
}

DataSetを取得するところですが、http://d.hatena.ne.jp/jyukutyo/20080828/1219885803をみてたら、 CSVファイルでも扱えるそうなのでCsvDataSetを使ってフィクスチャをCSVにすることにしました。 これならExcelでも扱えるしMacを使うプログラマにもイイ感じかと。   CsvDataSetのコンストラクタにはCSVファイルが置いてあるディレクトリを参照します。 そのディレクトリに table-ordering.txt というテキストファイルとCSVファイルを配置します。 table-ordering.txt は参照整合性制約に引っかからないように、 DBUnitCSVファイルを突っ込んでいくテーブルの順番を指定できるんだそうです。   まぁ、今回は1テーブルだけなので、、

item

んで、CSVは以下のように。

id,name,price,description
1,rerere,100,rererenore
2,gegege,200,gegegenokitaro

そうすると↓んな感じになります。

mysql> select * from item;
+----+--------+-------+----------------+
| id | name   | price | description    |
+----+--------+-------+----------------+
|  1 | rerere |   100 | rererenore     |
|  2 | gegege |   200 | gegegenokitaro |
+----+--------+-------+----------------+
2 rows in set (0.00 sec)

  あとは普通にテストコード書いていくだけなのですが、 TransactionAwareDataSourceProxyを使ってSpringとDBUnitを共存させなきゃ みたいなページをチラホラみかけました。   今回は参照用の機能のテストだけなのですが、追々更新系も入ってくると思うので、 その辺でトランザクション周りとSpringとDBUnitの共存まわりを調べたいと思います。