javajsonrestrest-assured

Rest Assured: java.lang.AssertionError: JSON path body doesn't match


The following is the JSON response I get when I hit a url:

{"status":"success","body":[{"id":1,"name":"ALL"},{"id":2,"name":"VW_CMPNY"},{"id":3,"name":"EDT_CMPNY"},{"id":4,"name":"ADD_CMPNY"},{"id":5,"name":"DLT_CMPNY"},{"id":6,"name":"VW_GRP"},{"id":7,"name":"EDT_GRP"},{"id":8,"name":"ADD_GRP"},{"id":9,"name":"DLT_GRP"},{"id":10,"name":"VW_ACCNT"},{"id":11,"name":"EDT_ACCNT"},{"id":12,"name":"ADD_ACCNT"},{"id":13,"name":"DLT_ACCNT"},{"id":14,"name":"VW_INVC"},{"id":15,"name":"EDT_INVC"},{"id":16,"name":"ADD_INVC"},{"id":17,"name":"DLT_INVC"},{"id":18,"name":"ON_RCRD"},{"id":19,"name":"OFF_RCRD"}]}

I'm checking that the JSON response is equal to a .json file I have; this is my code:

URI permissionsUri = new URI(permissionsUrl);
JSONParser jsonParser = new JSONParser();
Object obj = jsonParser.parse(new FileReader("\\db\\seed\\permission.json));
JSONArray expectedJson = (JSONArray) obj;
String expectedStatus = "success";
get(permissionsUri).then().assertThat().body("status", equalTo(expectedStatus)).and().body("body", equalTo(expectedJson));

But I'm getting the following error:

java.lang.AssertionError: JSON path body doesn't match. Expected: <[{"id":1,"name":"ALL"},{"id":2,"name":"VW_CMPNY"},{"id":3,"name":"EDT_CMPNY"},{"id":4,"name":"ADD_CMPNY"},{"id":5,"name":"DLT_CMPNY"},{"id":6,"name":"VW_GRP"},{"id":7,"name":"EDT_GRP"},{"id":8,"name":"ADD_GRP"},{"id":9,"name":"DLT_GRP"},{"id":10,"name":"VW_ACCNT"},{"id":11,"name":"EDT_ACCNT"},{"id":12,"name":"ADD_ACCNT"},{"id":13,"name":"DLT_ACCNT"},{"id":14,"name":"VW_INVC"},{"id":15,"name":"EDT_INVC"},{"id":16,"name":"ADD_INVC"},{"id":17,"name":"DLT_INVC"},{"id":18,"name":"ON_RCRD"},{"id":19,"name":"OFF_RCRD"}]> Actual: [{id=1, name=ALL}, {id=2, name=VW_CMPNY}, {id=3, name=EDT_CMPNY}, {id=4, name=ADD_CMPNY}, {id=5, name=DLT_CMPNY}, {id=6, name=VW_GRP}, {id=7, name=EDT_GRP}, {id=8, name=ADD_GRP}, {id=9, name=DLT_GRP}, {id=10, name=VW_ACCNT}, {id=11, name=EDT_ACCNT}, {id=12, name=ADD_ACCNT}, {id=13, name=DLT_ACCNT}, {id=14, name=VW_INVC}, {id=15, name=EDT_INVC}, {id=16, name=ADD_INVC}, {id=17, name=DLT_INVC}, {id=18, name=ON_RCRD}, {id=19, name=OFF_RCRD}]

I don't know why I'm getting the = in place of :. How can I solve this?


Solution

  • The "actual" part looks like the toString call on a collection or an array. You are comparing the contents of the JSONArray class against an actual JSON document. You have to serialize the JSONArray as a JSON document before comparing it against the response of your service.