原创

与 CustomComparator 比较时忽略 jsonassert 中的特定属性/字段

温馨提示:
本文最后更新于 2024年04月12日,已超过 48 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I need to compare two json strings but ignore some fields ("changeDate")

JSON 1

{
    "listInfo": {
        "offset": 0,
        "limit": 20,
        "count": 1
    },
    "items": [
        {
            "tsetId": 3,
            "name": "Bonus test",
            "description": "Bonus test description",
            "changeDate": "2024-04-09T09:14:27.899775",
            "changeUser": "user",
            "isDefault": false
        }
    ]
}

JSON 2

{
    "listInfo": {
        "offset": 0,
        "limit": 20,
        "count": 1
    },
    "items": [
        {
            "tsetId": 3,
            "name": "Bonus test",
            "description": "Bonus test description",
            "changeDate": "2024-03-03T04:44:17.1569775",
            "changeUser": "user",
            "isDefault": false
        }
    ]
}

I use JSONAssert and CustomComparator like this

JSONAssert.assertEquals(expectedResponseBody, actualResponseBody, //JSONCompareMode.LENIENT);
                new CustomComparator(JSONCompareMode.LENIENT, new Customization("**.changeDate", (o1, o2) -> true)
                ));

and I get an error

java.lang.AssertionError: items[changeDate=2024-04-09T09:14:27.899775]
Expected: a JSON object
     but none found
 ; items[changeDate=2024-03-03T04:44:17.1569775]
Unexpected: a JSON object

I tried to use "items.changeDate", "items[0].changeDate", "items[*].changeDate" in path in Customization and got the same error.

What is the right path in my case? Or may be the problem is somewhere else...

正文到此结束
热门推荐
本文目录