Come farei a assertThat
qualcosa è null
?Come affermare che qualcosa è nullo con Hamcrest?
ad esempio
assertThat(attr.getValue(), is(""));
Ma ottengo un errore che dice che non posso avere null
in is(null)
.
Come farei a assertThat
qualcosa è null
?Come affermare che qualcosa è nullo con Hamcrest?
ad esempio
assertThat(attr.getValue(), is(""));
Ma ottengo un errore che dice che non posso avere null
in is(null)
.
È possibile utilizzare metodo:
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
assertThat(attr.getValue(), is(nullValue()));
perché non utilizzare assertNull(object)
/assertNotNull(object)
?
+1 Generalmente preferisco le asserzioni di Hamscrest, ma questo è un caso in cui l'affermazione di Junit è appena più leggibile, IMO. – spaaarky21
assertThat() fornisce una registrazione molto migliore di molti altri metodi * assert *. Lo standard di codifica dei test che utilizzo favorisce assertThat() su tutti gli altri metodi di asserzione per questo motivo. – efelton
Il vantaggio principale quando si usa assertThat contro assertNul è che è più vicino a una frase pronunciata in inglese, basta provare a leggere qualsiasi delle asserzioni per controllarlo. – belgoros
Utilizza il seguente (da Hamcrest):
assertThat(attr.getValue(), is(nullValue()));
Se si vuole hamcrest
, si può fare
import static org.hamcrest.Matchers.nullValue;
assertThat(attr.getValue(), is(nullValue()));
In Junit
puoi fare
import static junit.framework.Assert.assertNull;
assertNull(object);
dice che il metodo nullValue() non è definito – user2811419
@ user2811419. Devi importare 'IsNull'. È un metodo statico in quella classe. Basta fare 'static import', o usare' IsNull.nullValue() '. –
Aggiungi 'import static org.hamcrest.core.IsNull.nullValue;' alla classe. –