Spring FrameworkのUnitテスト実装方法 1-5.Repositoryテスト(Junit4, spring-test, DBUnit, spring-test-dbunit) ※xlsxファイルからセットアップする
TERASOLUNA Server Framework for Java (5.x) Development Guideline
サンプルソースはこちら
(これのMyBatis3を使用したパターンで作成してます。)
1-5.Repositoryテスト Junit4, spring-test, DBUnit, spring-test-dbunitライブラリを使用したパターン ※xlsxファイルからセットアップする
今回は1-4をちょっと修正して、xmlファイルからセットアップする設定をエクセルファイル(xlsx)からセットアップする設定に書き換えるパターンです。
DBunitライブラリだけを使用するとちょっと面倒な感じになるのですが、spring-test-dbunitライブラリを使うことで、面倒くささが圧倒的に軽減されます。さすがです。
【pom.xml】
1-4の実装時と同じです。
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.5.4</version>
<scope>test</scope>
</dependency>
</dependencies>
【テスト対象クラス】
TodoRepository.java
テスト対象はupdateメソッド
public interface TodoRepository {
Todo findOne(String todoId);
Collection<Todo> findAll();
void create(Todo todo);
boolean update(Todo todo);
void delete(Todo todo);
long countByFinished(boolean finished);
}
【テストクラス】
TodoRepositoryTestVerSpringTestDBunitXlsx.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:META-INF/spring/test-context-dbunit.xml"})
//@TestExecutionListeners・・・TestContextManagerに指定したListenerを設定する。
//正直あまり良くわかってない。spring-test-dbunitを使う時はお決まり的なやつらしい。
//TransactionDbUnitTestExecutionListenerがspring-test-dbunitで必要だからかも。
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionDbUnitTestExecutionListener.class,
SqlScriptsTestExecutionListener.class
})
//@DbUnitConfiguration・・・自作したDataSetLoaderを使う時にこのアノテーションで設定する。
//xlsxファイルを読み込ませるようにしたDataSetLoaderを自作したため設定。
@DbUnitConfiguration(dataSetLoader = XlsDataSetLoader.class)
@Transactional
public class TodoRepositoryTestVerSpringTestDBunitXlsx {
@Inject
TodoRepository target;
@Inject
JdbcTemplate jdbctemplate;
@Before
public void setUp() {
//spring-test-dbunitアノテーションでセットアップと比較を行うため、処理なし
}
@Test
//@DatabaseSetup・・・spring-test-dbuniライブラリのアノテーション。テスト実行前にデータをセットアップしてくれる。テストクラスごと、メソッドごとでの指定が可能
@DatabaseSetup("classpath:META-INF/dbunit/test_data.xlsx")
//@ExpectedDatabase・・・spring-test-dbuniライブラリのアノテーション。テストメソッド実行後のテーブルの状態を指定したファイルと比較検証してくれる。
//エラーの時はJunitの例のバーが赤くなる。
@ExpectedDatabase(value="classpath:META-INF/dbunit/compare_data.xlsx", assertionMode = DatabaseAssertionMode.NON_STRICT)
public void testUpdate() {
//テスト用のデータを作成(getTodoDataメソッドはDBからデータを取得するprivateメソッド。取得したデータを書き換えて更新する。)
String todoId = "cceae402-c5b1-440f-bae2-7bee19dc17fb";
Todo testDataTodo = getTodoData(todoId);
testDataTodo.setFinished(true);
//updateメソッドのテスト
boolean actTodo = target.update(testDataTodo);
//結果検証
assertEquals(actTodo, true);
}
}
@DbUnitConfigurationアノテーションを新たに追加してます。
セットアップ時にデフォで設定されているファイルの形式はxml形式なのですが、
xlsxやcsvファイルでセットアップしたい!というときはこのアノテーションでdataSetLoaderクラスを書き換えてやります。
XlsDataSetLoader.java
//AbstractDataSetLoaderクラスを継承する
public class XlsDataSetLoader extends AbstractDataSetLoader {
//標準のxml形式からxlsl形式のフォーマットを読み込む設定に変更(override)する
@Override
protected IDataSet createDataSet(Resource resource) throws Exception {
// TODO 自動生成されたメソッド・スタブ
try(InputStream inputStream = resource.getInputStream()) {
return new XlsDataSet(inputStream);
}
}
}
こいつが今回のミソです。
AbstractDataSetLoaderクラスを継承させて、xlsxファイルを読み込ませるようにoverrideしています。
【その他設定】
test-context-dbunit.xml
DBunitを有効にするために少しtest-context.xml(1-1参照)を書き換えてます。
これをしないとDBunitがうまく動いてくれないらしい・・・
<bean id="log4jDataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">
<constructor-arg index="0" ref="realDataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg index="0" ref="log4jDataSource" />
</bean>
test_data.xlsx
セットアップ用のデータです。

シート名がテーブル名
1行目がカラム名
2行目以降がカラムに対してのデータ
になってます。
注意するべきなのは、
・エクセルのデフォルトでよくある「sheet1」とかは消すこと。
・カラム名やデータが入るところの表示形式を「文字列」に設定し、それ以外の表示形式は「標準」にすること。
例で言うと1行目~4行目のA列~D列は「文字列」で設定していて、それ以外は「標準」にしています。
そうしないとデータが空ですよ的なエラーが出ます。
compare_data.xlsx
テストメソッド実行後の検証用データです。
テストメソッドの内容が、ID”cceae402-c5b1-440f-bae2-7bee19dc17fb”のデータに対してFinishedをtrueにする内容なので、
ID=”cceae402-c5b1-440f-bae2-7bee19dc17fb”のFINISHEDを”true”に変更しています。

ということで、xlsxファイルの設定はちょこっと変えるだけで簡単に実装できます。
csvより簡単かもしれません。
サンプルソースはgithubで公開してます。
Spring-Unit-Test-pattern5


コメントを書く