0

当我遇到这个问题时,我正在使用GsonJSONAssert库。

我有以下函数来比较两个整数:

    private static void runCompareInts(int source, int target){

        JSONCompareResult result = JSONCompare.compareJSON(new Gson().toJson(source),
            new Gson().toJson(target),
            JSONCompareMode.NON_EXTENSIBLE);

        if (CollectionUtils.isEmpty(result.getFieldFailures())) {
            System.out.println("Objects are same.");
        } else {
            System.out.println("Objects are not the same. Difference: " + result);
        }

    }

当我运行时runCompareInts(1, 2),我得到"Objects are same."了结果,这不应该是这种情况。

我发现new Gson().toJson(1)返回 String "1",这是一个有效的 JSON 字符串,因此比较应该正确进行并进入else块。

比较整数 usingJSONAssert.assertNotEquals("1", "2", true)不会导致任何异常。这意味着 Gson 转换值不是问题。

谁能告诉我我的runCompareInts()功能有什么错误?谢谢你。

编辑:JSONAssert.assertNotEquals(new Gson().toJson(source), new Gson().toJson(target), true)也可以正常工作。

4

1 回答 1

0

result.isFailureOnField()在 if 块内部使用。

我看到 jsonassert 库中的消息存在问题,compareJson(final JSONString expected, final JSONString actual)类的方法JSONCompare在失败时返回空错误消息。

于 2018-06-11T10:17:28.443 回答