Jak byłbym assertThat
coś jest null
?Jak twierdzićCo coś jest nie tak z Hamcrestem?
np
assertThat(attr.getValue(), is(""));
Ale pojawia się błąd mówiąc, że nie mogę mieć null
w is(null)
.
Jak byłbym assertThat
coś jest null
?Jak twierdzićCo coś jest nie tak z Hamcrestem?
np
assertThat(attr.getValue(), is(""));
Ale pojawia się błąd mówiąc, że nie mogę mieć null
w is(null)
.
Można użyć metody:
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
assertThat(attr.getValue(), is(nullValue()));
dlaczego nie korzystać assertNull(object)
/assertNotNull(object)
?
+1 ja generalnie wolę twierdzeń Hamscrest ale jest to jeden przypadek, w którym twierdzenie Junit jest po prostu bardziej czytelny, IMO. – spaaarky21
assertThat() dają znacznie lepsze rejestrowanie niż wiele innych metod * potwierdzających. Testowy standard kodowania, którego używam, sprzyja temu assertThat() nad wszystkimi innymi metodami asercji z tego powodu. – efelton
Główną zaletą korzystania z assertThat vs assertNul jest to, że jest bliższe wypowiedziane w języku Englsih, po prostu spróbuj odczytać dowolne swoje twierdzenia, aby to sprawdzić. – belgoros
Użyj następujących (od Hamcrest):
assertThat(attr.getValue(), is(nullValue()));
Jeśli chcesz hamcrest
można zrobić
import static org.hamcrest.Matchers.nullValue;
assertThat(attr.getValue(), is(nullValue()));
W Junit
możesz zrobić
import static junit.framework.Assert.assertNull;
assertNull(object);
mówi, że metoda nullValue() nie jest zdefiniowana – user2811419
@ user2811419. Musisz zaimportować 'IsNull' Jest to metoda statyczna w tej klasie. Po prostu wykonaj 'static import' lub użyj' IsNull.nullValue() '. –
Dodaj 'import static org.hamcrest.core.IsNull.nullValue;' do swojej klasy. –