【发布时间】:2014-11-14 09:18:18
【问题描述】:
在我的 JUnit 测试中,我将预期的 XML 文件与解析器创建的虚拟文档进行比较:
public void test() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
try (InputStream input = getClass().getResourceAsStream("expected.xml");) {
Document expectedDocument = builder.parse(input);
Document actualDocument = createVirtualDocument();
XMLTestCase.assertXMLEqual(expectedDocument, actualDocument);
}
}
问题是:即使 XML 文件相等(或最后相似),断言也会失败。 XML 文件如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentScriptType="text/ecmascript" contentStyleType="text/css" preserveAspectRatio="xMidYMid meet" version="1.0" zoomAndPan="magnify">
<g class="group">
<rect class="shape" height="40.0" width="30.0" x="10.0" y="20.0"/>
</g>
</svg>
还有一个:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" contentScriptType="text/ecmascript" zoomAndPan="magnify" contentStyleType="text/css" preserveAspectRatio="xMidYMid meet" version="1.0">
<g class="group">
<rect x="10.0" width="30.0" height="40.0" y="20.0" class="shape"/>
</g>
</svg>
简而言之:属性顺序不同。我已经尝试过XMLUnit.setIgnoreAttributeOrder(true) 和XMLUnit.setNormalize(true),但无济于事。
测试失败时我得到的消息是:
[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg contentScriptType="text/ecmascript"...> at /svg[1]/@contentScriptType to <svg contentScriptType="text/ecmascript"...> at /svg[1]/@contentScriptType
[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg contentStyleType="text/css"...> at /svg[1]/@contentStyleType to <svg contentStyleType="text/css"...> at /svg[1]/@contentStyleType
[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg preserveAspectRatio="xMidYMid meet"...> at /svg[1]/@preserveAspectRatio to <svg preserveAspectRatio="xMidYMid meet"...> at /svg[1]/@preserveAspectRatio
[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg version="1.0"...> at /svg[1]/@version to <svg version="1.0"...> at /svg[1]/@version
[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg zoomAndPan="magnify"...> at /svg[1]/@zoomAndPan to <svg zoomAndPan="magnify"...> at /svg[1]/@zoomAndPan
[different] Expected presence of child node 'g' but was 'null' - comparing <g...> at /svg[1]/g[1] to at null
我也试过了:
Diff difference = new Diff(expectedDocument, actualDocument);
difference.overrideElementQualifier(new ElementNameAndAttributeQualifier());
但差异声称不相似。那么如何正确比较这两个 XML 文档呢?
【问题讨论】:
-
您会考虑另一种方法吗? (Unmarhsal XMLs with JAXB and the deep comparison of Java objects。)如果是的话,我可以把它画出来。 免责声明这将涉及我编写的一个开源库。
-
@lexicore 我认为 XMLUnit 的全部意义在于我不需要编写自己的比较代码?
-
对不起,我不知道为什么它不适用于 XMLUnit(否则我会回答你的问题)。您的代码似乎有效。我只是设置断点并进行调试。也许是一个命名空间问题,很难说。我在 XML 级别上非常成功地使用了 XMLUnit,通常它在相等/相同/相似等方面工作得很好。后来我将 mor 和更多切换到 JAXB 并使用自动生成的比较代码来比较未编组的对象。