XMLUnit断言等于XML不等于

时间:2014-11-14 09:18:19

标签: java xmlunit

在我的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文档呢?

1 个答案:

答案 0 :(得分:0)

那只是愚蠢的。如果我不比较Document但将它们转换为字符串并比较它们,则代码有效:

ByteArrayOutputStream expected = new ByteArrayOutputStream();
storeXml(expectedDocument, expected);

ByteArrayOutputStream actual = new ByteArrayOutputStream();
storeXml(actualDocument, actual);

assertXMLEqual(expected.toString(), actual.toString());

使用:

protected static void storeXml(Document document, OutputStream out) throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new DOMSource(document), new StreamResult(out));
}