java-hamcrest icon indicating copy to clipboard operation
java-hamcrest copied to clipboard

How to verify items in an array?

Open vegemite4me opened this issue 5 years ago • 0 comments

Could you please show how I can verify the contents of an array?

In the following example, how can I verify the contents of the cities array?

    @Test
    public void testArrayHasExpectedItems() throws Exception {
        String jsonAsString = "{" +
                "  \"cities\": [" +
                "    {" +
                "      \"name\": \"London\"," +
                "      \"size\": \"very big\"" +
                "    },{" +
                "      \"name\": \"Birmingham\"," +
                "      \"size\": \"big\"" +
                "    },{" +
                "      \"name\": \"Winchester\"," +
                "      \"size\": \"small\"" +
                "    }" +
                "  ]," +
                "  \"name\": \"United Kingdom\"" +
                "}";
        JsonNode json = new ObjectMapper().readTree(jsonAsString);

        assertThat(json, is(
                jsonObject()
                        .where("name", is(jsonText("United Kingdom")))
                        .where("cities", is(jsonArray()))

       // How do I verify cities contains London?
        ));
    }

vegemite4me avatar Sep 02 '20 10:09 vegemite4me