使用XMLUnit 2,如何比较两个文档并忽略元素顺序?
我看到了这个针对XMLUnit 1的问题,但显然v2中的新API不再具有该方法。
以下是我的当前代码:
Diff diff = DiffBuilder.compare(expected)
.withTest(actual)
.ignoreComments()
.ignoreWhitespace()
.checkForSimilar()
.build();
assertFalse(diff.hasDifferences());
针对 Stefan Bodewig 评论的编辑:
这是我在上面片段中进行比较的两个字符串:
String expected = "<root><foo>FOO</foo><bar>BAR</bar></root>";
String actual = "<root><bar>BAR</bar><foo>FOO</foo></root>";
报告的差异
Expected element tag name 'foo' but was 'bar' - comparing <foo...> at /root[1]/foo[1] to <bar...> at /root[1]/bar[1] (DIFFERENT)
Expected text value 'FOO' but was 'BAR' - comparing <foo ...>FOO</foo> at /root[1]/foo[1]/text()[1] to <bar ...>BAR</bar> at /root[1]/bar[1]/text()[1] (DIFFERENT)
Expected element tag name 'bar' but was 'foo' - comparing <bar...> at /root[1]/bar[1] to <foo...> at /root[1]/foo[1] (DIFFERENT)
Expected text value 'BAR' but was 'FOO' - comparing <bar ...>BAR</bar> at /root[1]/bar[1]/text()[1] to <foo ...>FOO</foo> at /root[1]/foo[1]/text()[1] (DIFFERENT)
DifferenceEvaluators.DEFAULT
的作用)。您确定您看到的是差异,而不是其他什么吗? - Stefan Bodewig