6

我正在成功尝试 XmlUnit,并且对我的工作非常有帮助。现在,我有一个小问题,我不知道如何解决。我有一个 java 类,它有一个 Set,当它转换成 XML 时,里面的元素可以有任何顺序。

当我在 XmlUnit 中尝试这两个文件时,它可以工作(Diff 说它们是相似的):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Monitor>
    <AvailableMeasures>
        <MeasureDescriptorA name="netInput_mynetwork"></MeasureDescriptorA>
        <MeasureDescriptor name="netInput_myothernetwork"></MeasureDescriptor>
    </AvailableMeasures>
</Monitor>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Monitor>
    <AvailableMeasures>
        <MeasureDescriptor name="netInput_myothernetwork"></MeasureDescriptor>
        <MeasureDescriptorA name="netInput_mynetwork"></MeasureDescriptorA>
    </AvailableMeasures>
</Monitor>

但是当标签具有相同的名称(具有不同的属性)时,它不起作用(它混合了属性,并期望另一个标签中的属性):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Monitor>
    <AvailableMeasures>
        <MeasureDescriptor name="netInput_myothernetwork"></MeasureDescriptor>
        <MeasureDescriptor name="netInput_mynetwork"></MeasureDescriptor>
    </AvailableMeasures>
</Monitor>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Monitor>
    <AvailableMeasures>
        <MeasureDescriptor name="netInput_mynetwork"></MeasureDescriptor>
        <MeasureDescriptor name="netInput_myothernetwork"></MeasureDescriptor>
    </AvailableMeasures>
</Monitor>

有什么解决方法吗?

4

3 回答 3

6

我自己找到了解决方案。

Diff diff = new Diff(controlXml, responseXml);
diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
于 2012-06-27T07:43:16.037 回答
3

这似乎适用于 XMLUnit 2.0:

    Diff myDiff = DiffBuilder.compare(Input.fromString(expected))
            .withTest(Input.fromString(actual))
            .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndAllAttributes))
            .checkForSimilar()
            .build();
于 2016-02-09T11:31:29.830 回答
1

上述解决方案仅适用于属性顺序,但如果您遇到相同元素类型顺序的问题,如下所示:

    <CustomerDataSet>
        <CustomerData>
            <Key>ACCOUNT_TYPE</Key>
            <Value>GREEN</Value>
        </CustomerData>
        <CustomerData name = "bla">
            <Key>EMAIL_ADDRESS</Key>
            <Value>MVNO_Automation_8@test.fr</Value>
        </CustomerData>
        <CustomerData>
            <Key>DATE_OF_BRITH</Key>
            <Value>01-01-1976</Value>
        </CustomerData>
    </CustomerDataSet>

            <CustomerDataSet>
        <CustomerData name = "bla">
            <Key>EMAIL_ADDRESS</Key>
            <Value>MVNO_Automation_8@test.fr</Value>
        </CustomerData>
        <CustomerData>
            <Key>ACCOUNT_TYPE</Key>
            <Value>GREEN</Value>
        </CustomerData>
        <CustomerData>
            <Key>DATE_OF_BRITH</Key>
            <Value>01-01-1976</Value>
        </CustomerData>
    </CustomerDataSet>

但是,您可以通过使用来解决此问题RecursiveElementNameAndTextQualifier

于 2015-12-24T12:06:19.600 回答