assertEquals对于以不同方式创建的等效Jackson对象失败

问题描述 投票:0回答:1

所以我想知道为什么两个对象以不同的方式创建时在我的单元测试(Junit 5)中都失败了。

第一方式:

static ObjectMapper mapper = new ObjectMapper();
JsonNode output = mapper.convertValue(jsonTransform, JsonNode.class);

第二种方式:

JsonNode expectedOutput = mapper.readTree(jsonString);

然后断言:

Assertions.assertEquals(expectedOutput, output);

失败:

is org.opentest4j.AssertionFailedError: expected: com.fasterxml.jackson.databind.node.ObjectNode@d6e7bab<jsonString> but was: com.fasterxml.jackson.databind.node.ObjectNode@5fa07e12<jsonString>

此外,如果我然后将断言编辑为:

Assertions.assertEquals(expectedOutput, mapper.readTree(output.toString()));

它将通过。那么什么概念导致第一个断言失败?

java junit jackson-databind
1个回答
0
投票

assertEquals将调用此方法来查找两个对象是否相等:

 private static boolean isEquals(Object expected, Object actual) {
        return expected.equals(actual);
    }

我将查看equals()中的JsonNode方法以查看正在检查的内容。

© www.soinside.com 2019 - 2024. All rights reserved.