What is the idiomatic Hamcrest pattern to assert that each element of an iterable matches a given matcher?

后端 未结 3 386
礼貌的吻别
礼貌的吻别 2020-12-14 07:17

Examine the following snippet:

    assertThat(
        Arrays.asList(\"1x\", \"2x\", \"3x\", \"4z\"),
        not(hasItem(not(endsWith(\"x\"))))
    );
         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 08:06

    You are looking for everyItem():

    assertThat(
        Arrays.asList("1x", "2x", "3x", "4z"),
        everyItem(endsWith("x"))
    );
    

    This produces a nice failure message:

    Expected: every item is a string ending with "x"
         but: an item was "4z"
    

提交回复
热议问题