什么是惯用的Hamcrest模式断言迭代的每个元素匹配给定的匹配器?

检查以下代码段:

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

这断言列表没有不以“x”结尾的元素。 当然,这是说明列表的所有元素都以“x”结尾的双重否定方式。

另请注意,该片段会引发:

 java.lang.AssertionError: Expected: not a collection containing not a string ending with "x" got:  

这列出了整个列表,而不仅仅是不以“x”结尾的元素。

那么有一种惯用的方式:

  • 断言每个元素以“x”结尾(没有双重否定)
  • 在断言错误时,仅列出那些不以“x”结尾的元素

David Harkness给出的匹配器为预期的部分产生了一个很好的信息。 但是, 实际部分的消息也取决于您使用的assertThat方法:

来自JUnitorg.junit.Assert.assertThat )的那个产生你提供的输出。

  • 使用not(hasItem(not(...)))匹配器:

     java.lang.AssertionError: Expected: not a collection containing not a string ending with "x" got: <[1x, 2x, 3x, 4z]> 
  • 使用everyItem(...)匹配器:

     java.lang.AssertionError: Expected: every item is a string ending with "x" got: <[1x, 2x, 3x, 4z]> 

来自Hamcrestorg.hamcrest.MatcherAssert.assertThat )的那个产生David给出的输出:

  • 使用not(hasItem(not(...)))匹配器:

     java.lang.AssertionError: Expected: not a collection containing not a string ending with "x" but: was <[1x, 2x, 3x, 4z]> 
  • 使用everyItem(...)匹配器:

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

我自己对Hamcrest断言的实验表明,“但是”部分经常令人困惑,这取决于多个匹配器的组合方式以及哪个匹配器首先失败,因此我仍然坚持使用JUnit断言,我完全知道我在哪里我会看到“得到”的部分。

您正在寻找everyItem()

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

这会产生一个很好的失败消息:

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

我知道这个问题已经很老了,但今天,使用Java 8,我宁愿用lambdas写它,例如

 Stream.of("1x", "2x", "3x", "4z").allMatch(e->e.endsWith("x")); 

这就是为什么。