Hamcrest Matchers.contains matcher not working (?)

前端 未结 1 1782
醉酒成梦
醉酒成梦 2021-01-17 22:10

I am trying to test if a collection has an item which toString() method returns a particular String. I tried do it using excellent Hamcrest matching classes, by combining co

相关标签:
1条回答
  • 2021-01-17 22:31

    I think you're looking for Matchers.hasItem(..)

    Assert.assertThat(items, Matchers.hasItem(Matchers.hasToString("c")));
    

    which states

    Creates a matcher for Iterables that only matches when a single pass over the examined Iterable yields at least one item that is matched by the specified itemMatcher. Whilst matching, the traversal of the examined Iterable will stop as soon as a matching item is found.

    Matchers.contains, as you stated,

    Creates a matcher for Iterables that matches when a single pass over the examined Iterable yields a single item that satisfies the specified matcher. For a positive match, the examined iterable must only yield one item.

    It seems to me like that's saying there should only be one element in the Iterable.

    0 讨论(0)
提交回复
热议问题