2013-09-24 3 views

答えて

192

あなたはメソッドを使用することができますすることができます

import static junit.framework.Assert.assertNull; 
assertNull(object); 
+0

メソッドnullValue()が定義されていません。 – user2811419

+1

@ user2811419 'IsNull'をインポートする必要があります。これは、そのクラスの静的メソッドです。 'static import'を実行するか、' IsNull.nullValue() 'を使用してください。 –

+0

クラスに 'import static org.hamcrest.core.IsNull.nullValue;'を追加します。 –

26

なぜ使用しないのですかassertNull(object)/assertNotNull(object)? (Hamcrestから)次

import static org.hamcrest.Matchers.is; 
import static org.hamcrest.Matchers.nullValue; 

assertThat(attr.getValue(), is(nullValue())); 
+5

+1私は一般的にHamscrestアサーションを好みますが、これはJunitアサーションがもっと読みやすいIMOの場合です。 – spaaarky21

+3

assertThat()は、他の多くのassert *メソッドよりも優れたロギングを提供します。私が使用するテストコーディング標準は、この理由のために他のすべてのアサーションメソッドよりもassertThat()を優先します。 – efelton

+0

assertThatとassertNulを使用するときの主な利点は、Englsihの話し言葉に近いことです。確認するためにあなたのアサーションのいずれかを読むようにしてください。 – belgoros

7

用途:あなたはhamcrestにしたい場合は

assertThat(attr.getValue(), is(nullValue())); 
11

、あなたがJunit

import static org.hamcrest.Matchers.nullValue; 

assertThat(attr.getValue(), is(nullValue())); 

行うことができます

関連する問題