'hasKey doesn't see the field in the response
I have test:
@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 year field contained the value 2001. But I get an exception:
Expected: map containing ["2001,"->ANYTHING]
Actual: [2000, 2001, 2002, 2003, 2004, 2005]
What am I doing wrong? In theory hasKey should return a single 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"
},
...
Solution 1:[1]
The error message give you the answers
Expected: map containing ["2001,"->ANYTHING]
Actual: [2000, 2001, 2002, 2003, 2004, 2005]
The actual result is an array, instead of a map. henceMatchers.hasItems
(match if Iterable
has items)
should be used instead ofMatchers.hasKeys
(match if the Map
has key)
Another problem is, since year
is Integer, 2001
should be the expected value instead of "2001"
.
So the assertion should be
.body("data.year", hasItems(2001))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |