How to assert list of object with RestAssured body and matcher hasItem

38 Views Asked by At

My endpoint returns List as JSON and I want to assert that the response contains correct transactions.

given().when()
       .get(path)
       .then()
       .statusCode(200)
       .header("Content-Type", containsString(MediaType.APPLICATION_JSON))
       .body("size()", is(4))
       .body("", hasItem(new TransactionDto("2005", "500", "month5 000")));

However, the code fails with:

java.lang.AssertionError: 1 expectation failed.
JSON path  doesn't match.
Expected: a collection containing <TransactionDto(date=2005, amount=500, description=month5 000)>
  Actual: <[{amount=500, date=2005, description=month5 000}, ...]>

Clearly they match, but 'Actual' is not the instance of TransactionDto. Is there a way how to either cast the 'Actual' to a class or a valid input for hasItem() so that it matches?

0

There are 0 best solutions below