Why doesn't he see the field in the answer?



  • Test available:

    @Test
        public void sixTest() {
            FlowerList flowerList = new FlowerList();
            Specification.installSpec(Specification.requestSpec(), Specification.responseSpec());
            Response response = given()
                    .when()
                    .get("/api/unknown")
                    .then()
                    .log().all()
                    .body("data.year", hasKey("2001"))
                    .extract().response().as((Type) FlowerList.class);
    

    I need to check that at least one field year has 2001. But I get an exception:

    Expected: map containing ["2001,"->ANYTHING]
      Actual: [2000, 2001, 2002, 2003, 2004, 2005]
    

    What am I doing wrong? Theoretically husKey has to return one value - 2001

    get:

    {
        page: 1,
        per_page: 6,
        total: 12,
        total_pages: 2,
        data: [
        {
        id: 1,
        name: "cerulean",
        year: 2000,
        color: "#98B2D1",
        pantone_value: "15-4020"
        },
        {
        id: 2,
        name: "fuchsia rose",
        year: 2001,
        color: "#C74375",
        pantone_value: "17-2031"
        },
        ... 
    


  • hasKey Check that there is a field/code with a specified value.
    This should be used. https://www.javadoc.io/doc/io.rest-assured/rest-assured/latest/io/restassured/RestAssured.html :

    // ...
        .body("data.year", hasItems("2000", "2001"))
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2