JSONCompareUtils: Full JSON Comparison Component for Automated Testing
JSONCompareUtils, a full‑JSON comparison component added to the Artemis framework, lets automated tests recursively compare large JSON responses via a Maven‑provided library with Java and Python wrappers, streamlining service‑splitting, BC‑traffic, and cross‑environment validation while dramatically cutting manual verification effort and speeding regression testing.
Background: With the deepening of left‑shift automated testing, many demands require full JSON response comparison between split code and the baseline release.
Practice results: The new automation platform added a full‑JSON comparison component, greatly improving testing efficiency in service‑splitting and BC‑traffic‑splitting projects.
Component details: The source component JSONCompareUtils, provided by the Artemis framework, offers precise comparison of large JSON strings using a recursive algorithm with configurable noise‑reduction.
Integration method: Add the Maven dependency <dependency> <groupId>com.dewu.tester</groupId> <artifactId>artemis</artifactId> <version>1.1.9‑SNAPSHOT</version> </dependency> and use the method JSONCompare(JSON expect, JSON actual, Properties properties) with parameters expect , actual , and properties .
Implementation example (Java):
public static Map
JSONCompare(JSON expect, JSON actual, Properties properties) {
Map
diffs = new HashMap<>();
if (null == expect && null == actual) {
return diffs;
} else if (expect instanceof JSONObject && actual instanceof JSONObject) {
diffs.putAll(JSONObjectCompare((JSONObject) expect, (JSONObject) actual, "$", properties));
} else if (expect instanceof JSONArray && actual instanceof JSONArray) {
diffs.putAll(JSONArrayCompare((JSONArray) expect, (JSONArray) actual, "$", properties));
} else {
diffs.put("$", (expect + COMPARE_ARROW + actual) + "not the same instance type");
}
if (!org.springframework.util.CollectionUtils.isEmpty(diffs)) {
for (Map.Entry
entry : diffs.entrySet()) {
logger.info("[key]" + entry.getKey() + "," + "[value]" + entry.getValue());
}
}
TrackingUtils.tracking();
return diffs;
}Python wrapper example for invoking the component:
import json, requests
def call(env_vars, g_vars, l_vars, sys_funcs, asserts, logger, **kwargs):
param = sys_funcs.get_call_param()
path = "http://******/artemis/component/interface-platform/compare/json"
actual1 = l_vars.get("json1")
actual2 = l_vars.get("json2")
headers = {"Content-Type": "application/json; charset=utf8"}
body = {
"expect": json.dumps(actual1, ensure_ascii=False),
"actual": json.dumps(actual2, ensure_ascii=False),
"properties": str(param["propeties"])
}
logger.info("Artemis请求body:" + str(body))
resq = requests.post(path, data=json.dumps(body), headers=headers, timeout=8)
res = json.loads(resq.text)
logger.info("======================artemis组件结果======================")
logger.info(res)
asserts.assertTrue(res["success"], msg="调用artemis-interface异常")
asserts.assertEqual(str(res["data"]), "{}", msg="存在不一致比对数据 :")
return resPractice process: Steps include extracting JSON1 and JSON2 from interfaces, adding the component, and performing the comparison.
Scenarios covered: service‑splitting validation, BC‑traffic splitting, cross‑environment comparison, and list‑order normalization using properties such as "$.data=order_no" or "$.data=merchantId".
Conclusion: The component significantly reduces manual verification effort and accelerates regression testing for large‑scale service‑splitting initiatives.
DeWu Technology
A platform for sharing and discussing tech knowledge, guiding you toward the cloud of technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.