ฉันสงสัยว่าใครรู้วิธีการตรวจสอบว่ารายการว่างเปล่าใช้assertThat()และMatchers?
วิธีที่ดีที่สุดที่ฉันเห็นเพียงใช้ JUnit:
assertFalse(list.isEmpty());แต่ฉันหวังว่าจะมีวิธีการทำเช่นนี้ใน Hamcrest
ฉันสงสัยว่าใครรู้วิธีการตรวจสอบว่ารายการว่างเปล่าใช้assertThat()และMatchers?
วิธีที่ดีที่สุดที่ฉันเห็นเพียงใช้ JUnit:
assertFalse(list.isEmpty());แต่ฉันหวังว่าจะมีวิธีการทำเช่นนี้ใน Hamcrest
คำตอบ:
มีอยู่เสมอ
assertThat(list.isEmpty(), is(false));... แต่ฉันเดาว่านั่นไม่ใช่สิ่งที่คุณหมายถึง :)
อีกวิธีหนึ่งคือ:
assertThat((Collection)list, is(not(empty())));empty()เป็นแบบคงที่ในMatchersชั้นเรียน สังเกตความจำเป็นที่จะต้องส่งผ่านlistไปCollectionด้วยขอบคุณนายพลผู้กล้าหาญ Hamcrest 1.2
การนำเข้าต่อไปนี้สามารถใช้กับ hamcrest 1.3
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.*;assertThat((Collection)list, is(not(empty())));
                    expected true but got falseคุณได้อะไรเช่นนี้expected empty but got [1, 2, 3]
                    assertThat(list, Matchers.<String>empty())(สมมติว่ารายการคือชุดของไฟล์String)
                    สิ่งนี้ได้รับการแก้ไขใน Hamcrest 1.3 โค้ดด้านล่างรวบรวมและไม่สร้างคำเตือนใด ๆ :
// given
List<String> list = new ArrayList<String>();
// then
assertThat(list, is(not(empty())));แต่ถ้าคุณต้องใช้รุ่นที่เก่ากว่า - แทนที่จะเป็นบั๊กempty()คุณสามารถใช้:
hasSize(greaterThan(0))
( import static org.hamcrest.number.OrderingComparison.greaterThan;หรือ
import static org.hamcrest.Matchers.greaterThan; )
ตัวอย่าง:
// given
List<String> list = new ArrayList<String>();
// then
assertThat(list, hasSize(greaterThan(0)));สิ่งที่สำคัญที่สุดเกี่ยวกับการแก้ปัญหาข้างต้นคือมันไม่ได้สร้างคำเตือนใด ๆ วิธีที่สองมีประโยชน์มากกว่าถ้าคุณต้องการประมาณขนาดผลลัพธ์ขั้นต่ำ
assertThat(list, not(hasSize(0)))จะประสบความสำเร็จถ้าlistเป็นnullเมื่อเทียบกับassertThat(list, hasSize(greaterThan(0)))
                    หากคุณหลังจากข้อความล้มเหลวที่สามารถอ่านได้คุณสามารถทำได้โดยไม่ต้อง hamcrest โดยใช้ assertEquals ปกติกับรายการว่าง:
assertEquals(new ArrayList<>(0), yourList);เช่นถ้าคุณวิ่ง
assertEquals(new ArrayList<>(0), Arrays.asList("foo", "bar");คุณได้รับ
java.lang.AssertionError
Expected :[]
Actual   :[foo, bar]แม้ว่าปัญหาทั่วไปจะได้รับการแก้ไขใน1.3สิ่งที่ยอดเยี่ยมเกี่ยวกับวิธีการนี้คือมันใช้ได้กับคลาสใดก็ตามที่มีisEmpty()วิธีการ! ไม่ใช่แค่Collections!
ตัวอย่างเช่นมันจะทำงานStringเช่นกัน!
/* Matches any class that has an <code>isEmpty()</code> method
 * that returns a <code>boolean</code> */ 
public class IsEmpty<T> extends TypeSafeMatcher<T>
{
    @Factory
    public static <T> Matcher<T> empty()
    {
        return new IsEmpty<T>();
    }
    @Override
    protected boolean matchesSafely(@Nonnull final T item)
    {
        try { return (boolean) item.getClass().getMethod("isEmpty", (Class<?>[]) null).invoke(item); }
        catch (final NoSuchMethodException e) { return false; }
        catch (final InvocationTargetException | IllegalAccessException e) { throw new RuntimeException(e); }
    }
    @Override
    public void describeTo(@Nonnull final Description description) { description.appendText("is empty"); }
}งานนี้:
assertThat(list,IsEmptyCollection.empty())