ComparisonFormatter - xmlunit/user-guide GitHub Wiki

ComparisonFormatter

The (I)ComparisonFormatter formats the differences between control and test XML in a human readable was as a String. Normally the default formatter will be used: org.xmlunit.diff.DefaultComparisonFormatter or Org.XmlUnit.Diff.DefaultComparisonFormatter

But you can also implement your own formatter. There are two methods to implement:

String getDescription(Comparison difference); // [1]
String getDetails(Detail details, ComparisonType type, boolean formatXml); // [2]

in Java, or for .NET:

string GetDescription(Comparison difference); // [1]
string GetDetails(Comparison.Detail details, ComparisonType type, bool formatXml); // [2]
  1. getDescription: should return a short human readable description what the differences are. Example: Return the String "Expected number of attributes '1' but was '2' - comparing <a...> at /a[1] to <a...> at /a[1]"
  2. getDetails(Detail, ComparisonType, boolean): should return the comparison details from the effected Test- or Control-Node. See the next section

getDetails

The default implementation DefaultComparisonFormatter returns the stringified XML snippet which contains the differences.

For Java this XML snippets are used by the org.xmlunit.matchers.CompareMatcher to create a org.junit.ComparisonFailure which can show the differences in your IDE as a nice Diff-View.

  • The Detail Argument contains either the details of the Control-Node or the Test-Node.
  • Depending on the ComparisonType of the differences the target-node, its parent or the whole document should be returned as XML-Snippet.
  • The boolean formatXml tells if the resulting XML should be formatted or not.
    For Example: if the input source was wrapped with a WhitespaceStrippedSource the resulting XML can and should be formatted to be human readable. But if the source was not wrapped by a content modification source, the formatting could remove/hide the real differences between test and control node.
⚠️ **GitHub.com Fallback** ⚠️