&tag(JUnit4);
*目次 [#u683eea8]
#contents
*参考情報 [#v91af51f]
-[[JUnitの新しいアサーション assertThat - etc9:http://d.hatena.ne.jp/Naotsugu/20090823/1251027380]]
-[[JUnit4 - TRANCE ARTS 技術情報Wiki:http://www.trance.co.jp/wiki/index.php?JUnit4]]
*assertThatとは [#cabd8a87]
**概要 [#wc149a59]
-Junit4.4から取り入れられたアサーションメソッド。
-org.hamcrest.Matcherと組み合わせて使う。
-○ is ×みたいな形式でかけるので英語ネイティブに分かりやすい利点があるらしい。さらにエラーメッセージが分かりやすいとか。
**メソッド定義 [#j650f4da]
-メソッドの定義は以下の通り。
#pre{{
public static <T> void assertThat(T actual, Matcher<T> matcher) {
assertThat("", actual, matcher);
}
public static <T> void assertThat(String reason, T actual,
Matcher<T> matcher) {
if (!matcher.matches(actual)) {
Description description= new StringDescription();
description.appendText(reason);
description.appendText("\nExpected: ");
description.appendDescriptionOf(matcher);
description.appendText("\n got: ");
description.appendValue(actual);
description.appendText("\n");
throw new java.lang.AssertionError(description.toString());
}
}
}}
-matcherは明示的に生成するのではなくis()やnot()のようなstaticファクトリメソッドで生成するのがいいらしい。
**使用イメージ [#l731397c]
-一個目が結果。二個目がMatcherを使った期待値となる。
#pre{{
assertThat(1 + 2, is(3));
}}
-部分文字列が含まれるかどうかの判定
#pre{{
assertThat("yes we have no bananas", containsString("bananas")); #=> OK
}}
*Eclipseで使用 [#eb1e15ba]
-新規テストケース追加時にJUnit4テストケースを選択するか、ライブラリでJUnit4ライブラリを追加できる。
*トラブルシューティング [#cc909544]
**@Testで実行できない [#c3631a06]
-JUnit4のテストはTestCaseを継承したらいけない。