eclipseでunitテストを書く前に最低限やっておくこと

メモでつ。eclipseGanymedeです。

1. quick-junitを入れる

http://quick-junit.sourceforge.jp/

  • テスティングペアを開く: Ctrl + 9
  • JUnit テスト:Ctrl + 0

で操作できる。

2. static importの展開をやめる

参考:http://d.hatena.ne.jp/Yamashiro0217/20090409/1239242490

Preferenceの[Java]-[Code Style]-[Organize Imports]の「Number of static imports needed for .*」を1に設定する。

3. GanymedeのテンプレートにassertThatとか追加する

参考:http://d.hatena.ne.jp/ashigeru/20090216/1234756826

Preferencesの[Java]-[Editor]-[Templates]から[New...]で追加できる。

assertThat
${assert:importStatic('org.junit.Assert.*','org.hamcrest.CoreMatchers.*')}
assertThat(${actual}, ${matcher})${cursor}

なるべく短い名前で参照したいのでnameはatとかにしておく。

before
@${Before:newType(org.junit.Before)}
public void setUp() throws Exception {
	${cursor}
}
after
@${After:newType(org.junit.After)}
public void tearDown() throws Exception {
	${cursor}
}

必要ならAfterClass、BeforeClassも追加する。ちなみにデフォルトでTestが以下のように登録されているので、これも活用する。

@${testType:newType(org.junit.Test)}
public void ${testname}() throws Exception {
	${staticImport:importStatic('org.junit.Assert.*')}${cursor} 
}

4. 使う

  1. command+nでNewのwindowを出して、testでフィルタリングして[JUnit Test Case]を選択して、適当にダイアログにしたがってテストケースを生成。
  2. テストケースはclass内でTestとかatとかbefore、afterとか入力してからctrl+spaceでテンプレートからコードを生成していく。
  3. 実行はQuick-JunitでCtrl+9とかでテスティングペアを開いたり、Ctrl+0で実行したりする。