I am comparing a saved example xml with a live marshalled xml in my JUnit testing. Validating the presence of a key value pair in the xml.
I am making use of XmlUnit 2.1.0 specifically
My xml is as follows:
<entries>
<entry>
<key>delete</key>
<value>ENABLED</value>
</entry>
<entry>
<key>view</key>
<value>DISABLED</value>
</entry>
<entry>
<key>create</key>
<value>DISABLED</value>
</entry>
</entries>
The order of the entries can vary. I'm unsure how to get it to validate correctly since it sees a different key value as a difference in the xml even though it's just an order change.
I am asserting similarity with the follow assertion in JUnit:
assertThat(marshalledXml, isSimilarTo(Input.fromFile("path/to/example.xml").ignoreWhitespace().ignoreComments());
I suspect I may need to make use of XPath matchers or the DefaultNodeMatchers with an ElementSelector.
Yes, you need to provide an ElementSelector
that "knows" which nodes to pick for comparison in your specific case.
For most of the document the name of the element seems to be what you should use. At least that's true for entries
, key
and value
. For entry
elements you want to compare those elements, that have matching nested text in the key
element that is their immediate child, right?
I think this translates to
ElementSelectors.conditionalBuilder()
.whenElementIsNamed("entry")
.thenUse(ElementSelectors.byXPath("./key", ElementSelectors.byNameAndText))
.elseUse(ElementSelectors.byName)
.build();
See https://github.com/xmlunit/user-guide/wiki/SelectingNodes for a more detailed discussion of the ElementSelector
options. Your XML is pretty close to the table
example used in the introduction and discussed in the next sections.