user1789685
user1789685

Reputation: 73

Assert that one JSON, which is like a list of maps, contains another JSON

I have a test that is trying to assert if the acutal json object, from the rest API call, contains the expected JSON, that I hard coded. My actual JSON that looks like

{
    "data": [{
        "requiredness": "required",
        "multi_value": false,
        "editable": true,
        "name": "name__v",
        "type": "String",
        "max_length": 50
    }, {
        "requiredness": "required",
        "multi_value": false,
        "editable": true,
        "name": "label__v",
        "type": "String",
        "max_length": 100
    }, {
        "requiredness": "required",
        "multi_value": false,
        "editable": true,
        "name": "active__v",
        "type": "Boolean"
    }, {
        "requiredness": "required",
        "component": "Doctype",
        "multi_value": false,
        "editable": true,
        "name": "type__v",
        "type": "Component"
    }, {
        "requiredness": "conditional",
        "component": "Doctype",
        "multi_value": false,
        "editable": true,
        "name": "subtype__v",
        "type": "Component"
    }, {
        "requiredness": "optional",
        "component": "Doctype",
        "multi_value": false,
        "editable": true,
        "name": "classification__v",
        "type": "Component"
    }, {
        "requiredness": "optional",
        "multi_value": false,
        "editable": true,
        "name": "filing_model__v",
        "type": "Object"
    }],
    "responseStatus": "SUCCESS"
}

And I want to assert that the following expected JSON contains only one map, from the list of the maps:

{  
   "data":[  
      {  
         "requiredness":"required",
         "multi_value":false,
         "editable":true,
         "name":"name__v",
         "type":"String",
         "max_length":50
      }
   ],
   "responseStatus":"SUCCESS"
}

This is my test

public void test(){
    JSONObject actualjson = callAPI(endpoint);
    JSONObject expectedjson =new JSONObject(expectedOutput);
    Assert.assertThat(actualjson.toString(),CoreMatchers.containsString(ExpectedjsonObj.toString()));
   } 

I get an exception due to the white spaces:

Expected: a string containing "{\"data\":[{\"requiredness\":\"required\",\"multi_value\":false,\"editable\":true,\"name\":\"name__v\",\"type\":\"String\",\"max_length\":50}],\"responseStatus\":\"SUCCESS\"}"

but what was obtained was

"{"data":[{"requiredness":"required","multi_value":false,"editable":true,"name":"name__v","type":"String","max_length":50},{"requiredness":"required","multi_value":false,

What is the best approach to assert that the list of maps in my actual JSON contains my expected maps( could be a list too)

Upvotes: 1

Views: 4100

Answers (1)

Brandon McKenzie
Brandon McKenzie

Reputation: 1685

Since you're testing the output of a REST API call, and order is not necessarily guaranteed in your output, the solution is exactly the thing you expressed that you did not want to do: assert the existence of every element in the JSONObject. Fortunately, the JSONAssert library may be utilized to do that for you.

Upvotes: 1

Related Questions