How can I compare map-like elements with XMLUnit

121 Views Asked by At

I have below xml data:

Control:

 <Data>

        <propertyValues>
            <propertyName>Name1</propertyName>
            <value>
                <text>
                    <value>Value1</value>
                </text>
            </value>
        </propertyValues>
        <propertyValues>
            <propertyName>Name2</propertyName>
            <value>
                <text>
                    <value>Value2</value>
                </text>
            </value>
        </propertyValues>
 </Data>

Test:

 <Data>
            <propertyValues>
                <propertyName>Name2</propertyName>
                <value>
                    <text>
                        <value>Value2</value>
                    </text>
                </value>
            </propertyValues>
            <propertyValues>
                <propertyName>Name1</propertyName>
                <value>
                    <text>
                        <value>Value1</value>
                    </text>
                </value>
            </propertyValues>
 </Data>

And I would expect these 2 documents are "same".

How can I config xmlUnit to make it work? (I'm using xmlunit 2.6.3)

Thanks

Leon

1

There are 1 best solutions below

4
Stefan Bodewig On BEST ANSWER

This is pretty similar to the running example of the "Selecting Nodes" part of XMLUnit's User Guide.

You need to use an ElementSelector that picks the correct propertyValues element when looking at the list of elements and then decides to compare the elements that contain the same nested text inside the only child element named propertyName. This directly translates into

ElementSelectors.conditionalBuilder()
    .whenElementIsNamed("propertyValues")
    .thenUse(ElementSelectors.byXPath("./propertyName", ElementSelectors.byNameAndText))
   ...

and then you need to add whatever other rules are required to make the rest work. Looking at the visible rest of your example there are no ambiguous children and a simple

   ...
    .elseUse(ElementSelectors.byName)
    .build();

will do.