比较 XMLUnit 2 中的子节点 - 预期子节点“node2”但为“null”

Kam*_*kol 1 xmlunit xmlunit-2

我需要使用XMLUnit 2xml 子节点顺序不同的位置来比较 XML 文件。由于使用了底层库,我无法影响子节点的顺序。

为了进行比较,我正在使用:

    <dependency>
        <groupId>org.xmlunit</groupId>
        <artifactId>xmlunit-core</artifactId>
        <version>2.0.0-alpha-02</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.xmlunit</groupId>
        <artifactId>xmlunit-matchers</artifactId>
        <version>2.0.0-alpha-02</version>
        <scope>test</scope>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

问题归结为这个 JUnit 测试:

import static org.hamcrest.MatcherAssert.assertThat;
import static org.xmlunit.builder.Input.fromString;
import static org.xmlunit.diff.ElementSelectors.byName;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;

import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.xmlunit.diff.DefaultNodeMatcher;

import java.io.IOException;

public class XmlTest {

    @Test
    public void test() throws Exception {
        String t1 = fromClassPath("t1.xml");
        String t2 = fromClassPath("t2.xml");
        assertThat(fromString(t1), isSimilarTo(fromString(t2)).withNodeMatcher(new DefaultNodeMatcher(byName)));
    }

    private static String fromClassPath(String fileName) throws IOException {
        return IOUtils.toString(new ClassPathResource(fileName).getInputStream());
    }
}
Run Code Online (Sandbox Code Playgroud)

其中t1.xml包含:

<root>
    <child>
        <node1/>
    </child>
    <child>
        <node2/>
    </child>
</root>
Run Code Online (Sandbox Code Playgroud)

t2.xml包含:

<root>
    <child>
        <node2/>
    </child>
    <child>
        <node1/>                                                                                                                        
    </child>
</root>
Run Code Online (Sandbox Code Playgroud)

实际上,node1node2是按顺序改变的。测试结果为:

java.lang.AssertionError: 
Expected: Expected child 'node2' but was 'null' - comparing <node2...> at /root[1]/child[1]/node2[1] to <NULL>:
<node2/>
     but: result was: 
<NULL>  
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以将此类 xml 文件与XMLUnit 2. 任何形式的帮助表示赞赏。

Ste*_*wig 5

您告诉 XMLUnit 按名称匹配元素,无需附加条件。在您的情况下,这意味着child元素按顺序进行比较。child显然,您需要一种不同的策略,即在决定选择哪个节点作为任何给定 的“伙伴”时查看节点的第一个子节点child

一种选择是

    new DefaultNodeMatcher(byXPath("./*[1]", byName), byName))
Run Code Online (Sandbox Code Playgroud)

它将首先通过尝试匹配它们的第一个子节点来匹配节点,并且 - 如果无法提出任何候选者 - 使用元素本身的名称。

根据实际文档的结构,这可能太天真,您需要使用条件元素选择器

    new DefaultNodeMatcher(selectorForElementNamed("child", byXPath("./*[1]", byName)), byName))
Run Code Online (Sandbox Code Playgroud)

注意:由于问题 #39,上述代码不适用于 XMLUnit 2.0.0-alpha-02 - 您必须使用 XPath 来"./child/*[1]"代替。后来已经byXPath ElementSelector修复了。

如果你不想使用XPath,你可以ElementSelector自己编写一个

public static class FirstChildElementNameSelector implements ElementSelector {
    @Override
    public boolean canBeCompared(Element controlElement,
                                 Element testElement) {
        return byName.canBeCompared(firstChildElement(controlElement),
                                    firstChildElement(testElement));
    }

    private Element firstChildElement(Element e) {
        NodeList nl = e.getChildNodes();
        int len = nl.getLength();
        for (int i = 0; i < len; i++) {
            if (nl.item(i) instanceof Element) {
                return (Element) nl.item(i);
            }
        }
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它

    new DefaultNodeMatcher(selectorForElementNamed("child", new FirstChildElementNameSelector()), byName))
Run Code Online (Sandbox Code Playgroud)