gpt4 book ai didi

java - XMLUnit 断言相等 XML 不相等

转载 作者:太空宇宙 更新时间:2023-11-04 14:23:27 25 4
gpt4 key购买 nike

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

最佳答案

这太愚蠢了。如果我不比较 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));
}

关于java - XMLUnit 断言相等 XML 不相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26926559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com