0

我正在使用 XMLUnit2 来比较 2 个没有相同顺序元素的 XML。我看到以下错误 -

差异 = 预期的子 'billingCode' 但为 'null' - 在 /translateServiceRequestResponse[1]/translateServiceRequestReturn[1]/legacyCode[2]/billingCode[1] 处比较为 NULL

代码

Diff myDiff = DiffBuilder.compare(controlResponse).ignoreWhitespace().ignoreComments().withTest(testResponse).withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)).checkForSimilar().build();
System.out.println("Differences = " + myDiff.toString());

控制文件

<translateServiceRequestResponse>
<translateServiceRequestReturn>
    <legacyCode>
        <amount>0</amount>
        <billingCode>VS128</billingCode>
        <description>HD Rec</description>
        <priceName>EquipChoice X1 HD TP</priceName>
        <pricingElementName>X1 HD Receiver</pricingElementName>
        <codeAmount>0</codeAmount>
        <lobSubType>0</lobSubType>
        <addressable>1</addressable>
        <nonStandard>false</nonStandard>
    </legacyCode>
    <legacyCode>
        <amount>0</amount>
        <billingCode>VF123</billingCode>
        <description>HD Rec</description>
        <packageCode>VE286</packageCode>
        <priceName>EquipChoice X1 HD TP</priceName>
        <pricingElementName>X1 HD Receiver</pricingElementName>
        <codeAmount>0</codeAmount>
        <lobSubType>0</lobSubType>
        <addressable>1</addressable>
        <nonStandard>false</nonStandard>
    </legacyCode>
    <legacyCode>
        <amount>0</amount>
        <billingCode>VF170</billingCode>
        <description>HD Rec</description>
        <packageCode>VE286</packageCode>
        <priceName>EquipChoice X1 HD TP</priceName>
        <pricingElementName>X1 HD Receiver</pricingElementName>
        <codeAmount>2.5</codeAmount>
        <lobSubType>0</lobSubType>
        <addressable>1</addressable>
        <nonStandard>false</nonStandard>
    </legacyCode>
</translateServiceRequestReturn>

测试文件

<translateServiceRequestResponse>
<translateServiceRequestReturn>
    <legacyCode>
        <amount>0</amount>
        <billingCode>VS128</billingCode>
        <description>HD Rec</description>
        <priceName>EquipChoice X1 HD TP</priceName>
        <pricingElementName>X1 HD Receiver</pricingElementName>
        <codeAmount>0</codeAmount>
        <lobSubType>0</lobSubType>
        <addressable>1</addressable>
        <nonStandard>false</nonStandard>
    </legacyCode>
    <legacyCode>
        <amount>0</amount>
        <billingCode>VF170</billingCode>
        <description>HD Rec</description>
        <packageCode>VE286</packageCode>
        <priceName>EquipChoice X1 HD TP</priceName>
        <pricingElementName>X1 HD Receiver</pricingElementName>
        <codeAmount>2.5</codeAmount>
        <lobSubType>0</lobSubType>
        <addressable>1</addressable>
        <nonStandard>false</nonStandard>
    </legacyCode>
    <legacyCode>
        <amount>0</amount>
        <billingCode>VF123</billingCode>
        <description>HD Rec</description>
        <packageCode>VE286</packageCode>
        <priceName>EquipChoice X1 HD TP</priceName>
        <pricingElementName>X1 HD Receiver</pricingElementName>
        <codeAmount>0</codeAmount>
        <lobSubType>0</lobSubType>
        <addressable>1</addressable>
        <nonStandard>false</nonStandard>
    </legacyCode>
</translateServiceRequestReturn>

4

1 回答 1

3

XMLUnit 使用ElementSelector来确定控制和测试文档中两组同级的哪些元素相互匹配。它将从文档的根开始匹配,您必须确保它在每个级别选择正确的分支。一旦做出决定,XMLUnit 就不会回溯。

选择一个文件时,ElementSelector您必须始终确保它有助于 XMLUnit 根据您的文档的逻辑结构要求将正确的分支尽可能靠近文档的根。你说的顺序不一样的元素就是legacyCode元素,所以你必须帮助XMLUnit在其中做出正确的选择。byNameAndText在这里帮不上忙。根本没有嵌套文本byNameAndText,所以它们都是相同的,并且 XMLUnit 按文档顺序匹配元素。

这与https://github.com/xmlunit/user-guide/wiki/SelectingNodestr中关于s 的问题完全相同

在我看来,好像您legacyCode会通过嵌套在billingCode元素中的文本来识别。如果是这样,您可以使用ElementSelector类似

ElementSelectors.conditionalBuilder()
    .whenElementIsNamed("legacyCode").thenUse(ElementSelectors.byXPath("./billingCode", ElementSelectors.byNameAndText))
    .elseUse(ElementSelectors.byName)
    .build();

您可能需要对此进行调整,以便它适用于您未显示的文档的某些部分,或者如果byNameAndText确实需要,而不是byName树的某些其他部分。

于 2017-05-20T05:54:47.560 回答