首页 > 解决方案 > 比较两个 json 文件时忽略特定属性

问题描述

我已经成功地使用 JSONAssert 来比较两个这样的 json 响应:

JSONAssert.assertEquals(response2.getResponseBodyContent(), response1.getResponseBodyContent(), JSONCompareMode.LENIENT)

我现在需要忽略此处描述的某些属性:

在比较两个 JSON 时忽略特定节点/属性 我的新声明是:

JSONAssert.assertEquals(response2, getResponseBodyContent(), new CustomComparator(JSONCompareMode.LENIENT, new Customization("EffectiveEpochDate", (o1, o2) -> true)));

我收到以下错误:

java.lang.Error: Unresolved compilation problems:   
Groovy:expecting ')', found ',' @ line 51, column 154.  
Groovy:expecting ')', found '->' @ line 51, column 160.     
Groovy:expecting ')', found '->' @ line 51, column 160.     
Groovy:expecting '}', found '->' @ line 51, column 160.     
Groovy:expecting '}', found '->' @ line 51, column 160.

我正在使用一个名为 Katalon 的测试工具,它支持 java/groovy。任何输入将不胜感激。谢谢

标签: javajsongroovyjsonassert

解决方案


The code you are referencing uses the Java lambda syntax (which for sure is not supported up to and including Groovy 2.5). You have to pass an closure instead. E.g. turn

(o1, o2) -> true

into:

{a, b -> true}

推荐阅读