ハムクレスト – どのようにアサーションするのはnull値をチェックするのですか?
Hamcrest
`assertThat`アサーションでヌル値をチェックしてみてください。
@Test
public void testApp()
{
//ambiguous method call?
assertThat(null, is(null));
}
1.解決策
1.1ヌル値をチェックするには `is(nullValue)`を試してください。static import `org.hamcrest.CoreMatchers。** `
//hello static import, nullValue here
import static org.hamcrest.CoreMatchers.** ;
import org.junit.Test;
//...
@Test
public void testApp()
{
//true, check null
assertThat(null, is(nullValue()));
//true, check not null
assertThat("a", is(notNullValue()));
}
1.2またはこれと同等のもの
import static org.hamcrest.CoreMatchers.** ;
import org.hamcrest.core.IsNull;
import org.junit.Test;
//...
@Test
public void testApp()
{
//true, check null
assertThat(null, is(IsNull.nullValue()));
//true, check not null
assertThat("a", is(IsNull.notNullValue()));
}
上記(1.1)の `CoreMatchers.nullValue()`は、 `IsNull.nullValue`の省略形です。次のソースコードを見直してください:
org.hamcrest.CoreMatchers.java
public static org.hamcrest.Matcher<java.lang.Object> nullValue() {
return org.hamcrest.core.IsNull.nullValue();
}
P.S `notNullValue()`と同じです
参考文献
-
http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/core/IsNull.html
[Hamcrest-
IsNull JavaDoc]。
ハムクレストオフィシャルサイト
-
リンク://タグ/アサーション/[アサーション]リンク://タグ/hamcrest/[hamcrest]