Search code examples
groovyassertmatcherhamcrest

Groovy - how to match (assert) that a certain value is contained in an array of expected values


I have a script which checks returned http status codes.

import static org.hamcrest.Matchers.anyOf
import static org.hamcrest.Matchers.equalTo
import static org.hamcrest.MatcherAssert.assertThat

int[] expectedStatuses = [201,204]
def pollStatusCode = 202
def actualStatusCode = 201

How to assert that actualStatusCode is contained in an array of expectedStatuses' values? Something like:

assertThat(actualStatusCode, anyOf(equalTo(pollStatusCode), equalTo(expectedStatuses)))

Is there any way how to assert this type of values?


Solution

  • assert actualStatusCode in expectedStatuses
    

    or

    assert expectedStatuses.contains(actualStatusCode)