ฉันจะทำassertThatยังnullไงดี?
ตัวอย่างเช่น
assertThat(attr.getValue(), is(""));
แต่ฉันได้รับข้อผิดพลาดบอกว่าฉันไม่สามารถมีในnullis(null)
ฉันจะทำassertThatยังnullไงดี?
ตัวอย่างเช่น
assertThat(attr.getValue(), is(""));
แต่ฉันได้รับข้อผิดพลาดบอกว่าฉันไม่สามารถมีในnullis(null)
คำตอบ:
คุณสามารถใช้IsNull.nullValue()วิธีการ:
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
assertThat(attr.getValue(), is(nullValue()));
IsNullมันเป็นวิธีการคงที่ในชั้นเรียนนั้น เพียงแค่ทำหรือการใช้งานstatic import IsNull.nullValue()
import static org.hamcrest.core.IsNull.nullValue;ในชั้นเรียนของคุณ
import static org.hamcrest.CoreMatchers.nullValueที่มีการเปลี่ยนแปลงและคุณจำเป็นต้อง
ทำไมไม่ใช้assertNull(object)/ assertNotNull(object)?
ถ้าคุณต้องการhamcrestคุณสามารถทำได้
import static org.hamcrest.Matchers.nullValue;
assertThat(attr.getValue(), is(nullValue()));
ในJunitสิ่งที่คุณสามารถทำได้
import static junit.framework.Assert.assertNull;
assertNull(object);
ใช้สิ่งต่อไปนี้ (จาก Hamcrest):
assertThat(attr.getValue(), is(nullValue()));
ใน Kotlin isสงวนไว้ให้ใช้:
assertThat(attr.getValue(), `is`(nullValue()));
isหรือไม่?