Search code examples
javaassert

How to assert on the existence of a certain value in an array


class Configuration { String name, String type }
Configuration[] configurations = ..

I want to put a assert check to see if the array contains a certain value in the name field (say name="Any"). We are currently looping the array to check this, what would be an elegant way


Solution

  • Using the Streams API:

    assertTrue(Arrays.stream(configurations).anyMatch(c -> "Any".equals(c.name));