我是JBehave Rest自动化脚本的新手。下面我编写了几行代码,其中我的要求是将每个实际的JSON字段数据与预期的数据进行比较。在这里,JSON中的字段数量是巨大的,我如何才能以最好的方式编写脚本来处理这种情况。
JSONObject actualjson = new JSONObject(actualJsonresponse);
JSONArray actualjsonData = actualjson.getJSONArray("outputDtlList");
JSONObject expectedjson = new JSONObject(RTRestServicesBean.getConfigurationJsonConfigValue());
JSONArray expectedjsonData = expectedjson.getJSONArray("outputDtlList");
String actual_storagetype = actualjsonData.getJSONObject(0).getString("storageType");
String expected_storagetype = expectedjsonData.getJSONObject(0).getString("storageType");
Assert.assertEquals(actual_storagetype, expected_storagetype);
String actual_locnNbr = actualjsonData.getJSONObject(0).getString("locnNbr");
String expected_locnNbr = expectedjsonData.getJSONObject(0).getString("locnNbr");
Assert.assertEquals(actual_locnNbr, expected_locnNbr);发布于 2018-01-06 05:26:30
考虑使用net.javacrumbs.json-unit:json-unit。
import static net.javacrumbs.jsonunit.JsonAssert.*;
...
assertJsonEquals(expectedjson, actualjsonData);发布于 2018-01-06 09:23:30
考虑切换到Karate,它可以更好地处理完整的JSON有效负载比较:
* def json = { foo: 'world', hey: 'ho', zee: [1, 2, 3] }
* remove json.hey
* match json == { foo: 'world', zee: [1, 2, 3] }https://stackoverflow.com/questions/48105630
复制相似问题