jilt3d
jilt3d

Reputation: 4034

Testing with jsonPath for repeating values which are contained in a list of value (Hamcrest)

I have the following json structure:

{
  content: [
    {
      status: 100,
    },
    {
      status: 100,
    },
    {
      status: 200,
    },
    {
      status: 300
    }
  ]
}

I use the following code in Spring to test that any status value from the json is withing accepted values specified with an array of statuses:

jsonPath("$.content[*].status", Matchers.containsInAnyOrder(100, 200, 300));

it fails because the examined iterable is not of the same length as the number of specified items. If I specify the array like this Matchers.containsInAnyOrder(100, 100, 200, 300), then it succeeds.

I checked the implementation of the matcher, and it looks like for each successful match, the matched value is removed from the specified items.

Is there any Hamcrest matcher that will not remove the matched item?

Upvotes: 3

Views: 1981

Answers (1)

zeppelin
zeppelin

Reputation: 9355

Matchers.containsInAnyOrder

Creates an order agnostic matcher for Iterables that matches when a single pass over the examined Iterable yields a series of items, each logically equal to one item anywhere in the specified items.

But what you are really trying to do, is check if every item is within a valid set of values, which could be written as that:

Matchers.everyItem(Matchers.isOneOf(100,200,300))

Upvotes: 4

Related Questions