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

后端 未结 3 385
礼貌的吻别
礼貌的吻别 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 07:53

    The matcher given by David Harkness produces a nice message for the expected part. The message for the actual part, however, is also determined by which assertThat method you use:

    The one from JUnit (org.junit.Assert.assertThat) produces the output you provided.

    • With the not(hasItem(not(...))) matcher:

      java.lang.AssertionError: 
      Expected: not a collection containing not a string ending with "x"
           got: <[1x, 2x, 3x, 4z]>
      
    • With the everyItem(...) matcher:

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

    The one from Hamcrest (org.hamcrest.MatcherAssert.assertThat) produces the output given by David:

    • With the not(hasItem(not(...))) matcher:

      java.lang.AssertionError: 
      Expected: not a collection containing not a string ending with "x"
           but: was <[1x, 2x, 3x, 4z]>
      
    • With the everyItem(...) matcher:

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

    My own experimentation with the Hamcrest assert showed me that the "but" part is often confusing, depending on how exactly multiple matchers are combined and which one fails first, and therefore I still stick to the JUnit assert, where I know quite exactly what I'll see in the "got" part.

    0 讨论(0)
  • 2020-12-14 07:57

    I know this question is quite old, but today, with Java 8, I'd rather write it with lambdas, e.g.

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

    This is why.

    0 讨论(0)
  • 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"
    
    0 讨论(0)
提交回复
热议问题