Comparing similar xml files with XmlUnit with unordered tags (same tag name with different attributes)

后端 未结 3 1924
囚心锁ツ
囚心锁ツ 2020-12-18 05:29

I am trying successfully XmlUnit, and is very helpful in my job. Now, I have a little problem, that I don\'t know how to solve. I have a java class, that has a Set, and when

相关标签:
3条回答
  • 2020-12-18 06:17

    Above solution only works with attributes order but it will not work if you have issues with the same element type order like below:

        <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>
    

    However, you can work around this by using RecursiveElementNameAndTextQualifier instead.

    0 讨论(0)
  • 2020-12-18 06:24

    I found the solution by myself.

    Diff diff = new Diff(controlXml, responseXml);
    diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
    
    0 讨论(0)
  • 2020-12-18 06:27

    This seems to work for XMLUnit 2.0:

        Diff myDiff = DiffBuilder.compare(Input.fromString(expected))
                .withTest(Input.fromString(actual))
                .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndAllAttributes))
                .checkForSimilar()
                .build();
    
    0 讨论(0)
提交回复
热议问题