Quick revision
The short answer
We build a Diff with DiffBuilder.compare(expected).withTest(actual), add ignoreWhitespace(), a node filter that drops the id element, and a DefaultNodeMatcher whose element selector matches sibling items by content instead of position (byNameAndText for simple elements, or byXPath on a key child for structured ones), and call checkForSimilar(). Then assertFalse(diff.hasDifferences(), diff.toString()) fails with a readable list of differences; namespaces are compared by URI, so different prefixes do not cause failures.
What to remember
In this answer, we compared a SOAP response with XMLUnit's DiffBuilder. ignoreWhitespace() removed formatting noise, a node filter dropped the generated id element by local name, and a DefaultNodeMatcher with byNameAndText, or a conditional byXPath selector for structured items, paired siblings by content so their order did not matter. checkForSimilar() accepted order and prefix differences while keeping real ones, assertFalse(diff.hasDifferences(), diff.toString()) gave readable failures, and namespaces were compared by URI. xmlunit-assertj expresses the same check fluently.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We build a Diff with DiffBuilder.compare(expected).withTest(actual), add ignoreWhitespace(), a node filter that drops the id element, and a DefaultNodeMatcher whose element selector matches sibling items by content instead of position (byNameAndText for simple elements, or byXPath on a key child for structured ones), and call checkForSimilar(). Then assertFalse(diff.hasDifferences(), diff.toString()) fails with a readable list of differences; namespaces are compared by URI, so different prefixes do not cause failures.
Why this question is asked
SOAP and other XML integrations are still everywhere in enterprise Java, and testing them by comparing strings fails for the same reasons as with JSON, plus a few that are specific to XML: namespace prefixes, whitespace between elements, and order of repeated elements. Interviewers ask this to see whether we can configure XMLUnit precisely rather than just call it. In this answer, we will look at the documents, recap how XMLUnit compares, then add each piece of the configuration, whitespace, the id filter and order-independent matching, see why similar is the right level, and finish with namespaces and the AssertJ variant.
1The Documents We Compare
The service returns an order with a generated id, two items in whatever order it likes, a different namespace prefix from the one in our expected file, and pretty-printing whitespace. All of that is noise for our test; only the items matter.
2A Quick Recap of How XMLUnit Compares
XMLUnit 2 parses both documents and walks them together. At each level it must decide which node in the test document corresponds to which node in the control (expected) document. That decision is made by a node matcher. By default, elements are matched in order by name, so the first item is paired with the first item. Then it compares the paired nodes and records every difference it finds, each with a type, such as TEXT_VALUE, CHILD_NODELIST_SEQUENCE or NAMESPACE_PREFIX.
Each difference is classified as EQUAL, SIMILAR or DIFFERENT. Similar means the documents differ in a way that does not change their meaning: a different namespace prefix for the same URI, CDATA instead of plain text, or children that are matched but appear in a different order. That classification is what lets us say 'ignore order, but not content'.
- Parse control and test documents
- Node filter: is this an id element?YesSkip it entirelyNoKeep comparing
- Whitespace-only text?YesIgnored (ignoreWhitespace)NoCompare the text
- Node matcher pairs sibling itemsby the text of their sku child
- Is the difference only SIMILAR?YesAccepted (order, prefix)NoReported as a difference
5 steps in order. Play them, or step through them one at a time.
3Writing the Diff
Here is the full assertion. Each line handles one kind of noise.
ignoreWhitespace() trims text and removes text nodes that contain only whitespace, so pretty-printed and compact documents compare equal.
withNodeFilter() decides which nodes take part in the comparison at all. Our filter keeps every node except elements whose local name is id, so the generated value is never looked at. We use getLocalName(), the name without the prefix, because the prefixes differ between the documents. Notice that our filter replaces XMLUnit's default filter, which skips document type declarations; if the documents had DOCTYPEs, we would combine the two.
withNodeMatcher() is what makes sibling order irrelevant. ElementSelectors.byNameAndText pairs elements that have the same name and the same text, which is perfect for simple siblings such as <tag>red</tag><tag>blue</tag>. Our items have child elements instead of text, so we use a conditional selector: for item elements, pair them by the text of their sku child, using byXPath with a prefix mapping; for everything else, match by name. Now the A1 item in the expected document is paired with the A1 item in the actual one, wherever it appears. Check the exact overloads for your XMLUnit version.
4Why checkForSimilar() Is the Right Level
Even with the right pairing, the two items appear in a different order, and the prefixes are ns and o. XMLUnit records these as differences of type CHILD_NODELIST_SEQUENCE and NAMESPACE_PREFIX, classified as SIMILAR. checkForSimilar() tells the Diff to keep only differences that are DIFFERENT, so these are accepted. Without it, the Diff checks for identical documents and would report them.
When something real differs, the message is precise. If the A1 quantity were 3, the assertion would fail with a description along the lines of 'Expected text value 2 but was 3 - comparing <ns:qty ...>2</ns:qty> at /order[1]/item[1]/qty[1]/text()[1] to <o:qty ...>3</o:qty> at /order[1]/item[2]/qty[1]/text()[1]'. Notice that it reports both XPaths, item[1] in the control and item[2] in the test, which confirms that the matcher paired the items by sku rather than by position.
5Namespaces and the AssertJ Variant
XML namespaces are identified by their URI; the prefix is only a local abbreviation. XMLUnit compares element names by namespace URI and local name, so ns:item and o:item with the same URI are the same element, and the prefix difference is merely similar. That avoids a whole class of false failures when a different SOAP stack picks different prefixes. The opposite is also true: an element in the right local name but the wrong namespace URI is a real difference, which is usually a real bug in a SOAP integration.
If the tests use AssertJ, the xmlunit-assertj module offers the same configuration as a fluent assertion.
6Follow-up questions
Could we ignore the id with XPath instead of a node filter?
Yes. Another option is a DifferenceEvaluator that downgrades differences at the id's XPath to similar, which keeps the element in the comparison but accepts any value. A node filter is simpler when the element should be ignored completely; a difference evaluator is useful when we still want to check that the element exists.
What happens if no element selector matches two items?
XMLUnit reports the unmatched control item as missing and the unmatched test item as unexpected, with CHILD_LOOKUP differences. That is the right outcome: if the response has item C3 instead of B7, the test should fail and say so.
7Conclusion
In this answer, we compared a SOAP response with XMLUnit's DiffBuilder. ignoreWhitespace() removed formatting noise, a node filter dropped the generated id element by local name, and a DefaultNodeMatcher with byNameAndText, or a conditional byXPath selector for structured items, paired siblings by content so their order did not matter. checkForSimilar() accepted order and prefix differences while keeping real ones, assertFalse(diff.hasDifferences(), diff.toString()) gave readable failures, and namespaces were compared by URI. xmlunit-assertj expresses the same check fluently.
Open with
I would use XMLUnit 2's DiffBuilder: compare the expected document, withTest the actual one. Then I configure each kind of noise. ignoreWhitespace() handles indentation and line breaks. For the generated id, I add withNodeFilter with a predicate that drops elements whose local name is id; I use the local name because prefixes can differ.
Then, if they want more
For sibling order, I set a node matcher: new DefaultNodeMatcher with an element selector that pairs elements by content instead of position. ElementSelectors.byNameAndText works for simple elements with text, and for structured items I would use a conditional selector that matches item elements by the text of a key child, like sku, through byXPath.
Then checkForSimilar(), because order differences and namespace prefix differences are classified as similar, and I only want real differences reported. Finally, assertFalse(diff.hasDifferences(), diff.toString()), so the failure message lists each difference with the XPath in both documents.
On namespaces, XMLUnit compares names by namespace URI and local name, so different prefixes for the same URI do not cause false failures, while a wrong namespace URI is still caught. If the team uses AssertJ, xmlunit-assertj gives the same thing as XmlAssert.assertThat(actual).and(expected) with the same options and areSimilar().
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A SOAP integration returns XML with namespaces, a generated id and elements in varying...": https://scrivio-interview-library.netlify.app/java/junit/44fbd8c237/