DiffBuilder - xmlunit/user-guide GitHub Wiki
With the DiffBuilder you can build the difference between two XML Sources in human readable fluent style.
By default the Diff
returned by DiffBuilder
contains DIFFERENT
and SIMILAR
Difference
s. If you want to ignore SIMILAR
Difference
s
use checkForSimilar
.
This example will throw an AssertionError: "Expected attribute value 'abc' but was 'xyz'".
final String control = "<a><b attr=\"abc\"></b></a>";
final String test = "<a><b attr=\"xyz\"></b></a>";
Diff myDiff = DiffBuilder.compare(Input.fromString(control))
.withTest(Input.fromString(test))
.build();
Assert.assertFalse(myDiff.toString(), myDiff.hasDifferences());
the C# equivalent would be
string control = "<a><b attr=\"abc\"></b></a>";
string test = "<a><b attr=\"xyz\"></b></a>";
var myDiff = DiffBuilder.Compare(Input.FromString(control))
.WithTest(Input.FromString(test))
.Build();
Assert.IsFalse(myDiff.HasDifferences(), myDiff.ToString());
The name Diff
is a class and a namespace name in XMLUnit.NET which
may force you to alias the class name in using
statements. In the
examples we'll always use var
to avoid the ambiguity.
You can compare all kind of Objects with each other which can be used as a XML source.
See: Providing Input To XMLUnit - Input.from(Object)
The DiffBuilder
can be configured via fluent API:
Diff myDiff = DiffBuilder.compare(control)
.withTest(test)
.checkForSimilar().checkForIdentical() // [1]
.ignoreComments() // [2]
.ignoreWhitespace() // [3]
.normalizeWhitespace() // [4]
.withComparisonController(ComparisonController) // [5]
.withComparisonFormatter(comparisonFormatter) // [6]
.withComparisonListeners(comparisonListeners) // [7]
.withDifferenceEvaluator(differenceEvaluator) // [8]
.withDifferenceListeners(comparisonListeners) // [9]
.withNodeMatcher(nodeMatcher) // [10]
.withAttributeFilter(attributeFilter) // [11]
.withNodeFilter(nodeFilter) // [12]
.withNamespaceContext(map) // [13]
.withDocumentBuilerFactory(factory) // [14]
.ignoreElementContentWhitespace() // [15]
-
checkForSimilar() checkForIdentical():
The default ischeckForIdentical
and leads to aDiff
that containsDIFFERENT
andSIMILAR
Difference
s. WhencheckForSimilar
is used,SIMILAR
Difference
s are ignored. The Difference between identical and similar is decided by the default DifferenceEvaluator. -
ignoreComments():
will stripping all comments from the test- and control-XML before comparing. -
ignoreWhitespace():
will removing all empty text nodes and trimming the non-empty ones from the test- and control-XML before comparing. -
normalizeWhitespace():
will removing all empty text nodes and normalizing the non-empty ones from the test- and control-XML before comparing. With "normalized" in this context means all whitespace characters are replaced by space characters and consecutive whitespace characters are collapsed. -
withComparisonController():
The default ComparisonController will let the DifferenceEngine evaluate all differences between test and control XML.
If the evaluation should stop after the first found difference you must use theComparisonControllers.StopWhenDifferent
instead.
See withComparisonController() -
withComparisonFormatter():
Use a custom Formatter for the Error Messages.
See ComparisonFormatter. -
withComparisonListeners():
Registers a listener that is notified of each comparison.
See ComparisonListener. -
withDifferenceEvaluator():
Provide your own custom DifferenceEvaluator implementation.
See DifferenceEvaluator. -
withDifferenceListeners():
Registers a listener that is notified of each comparison with outcome other thanComparisonResult.EQUAL
.
See ComparisonListener. -
withNodeMatcher():
Sets the strategy for selecting nodes to compare.
See NodeMatcher. - withAttributeFilter(): Optional strategy that allows certain attributes to be ignore completely. See AttributeFilter.
- withNodeFilter(): Optional strategy that allows certain nodes to be ignore completely. See NodeFilter.
-
withNamespaceContext():
Establish a namespace context that will be used inComparison.Detail.getXPath()
.
Without a namespace context (or with an empty context) the XPath expressions will only use local names for elements and attributes.
See NamespaceContext -
withDocumentBuilderFactory():
Use the given
DocumentBuilderFactory
when creating a document from the test or control source. Only available in XMLUnit for Java - since XMLUnit 2.2.0. - ignoreElementContentWhitespace(): will removing all text nodes solely consisting of whitespace (AKA as element content whitespace) - since XMLUnit 2.6.0.
the .NET version is almost the same, only the method names are capitalized.
Assuming you compare two XMLs with two differences (attr1 and attr2):
final String control = "<a><b attr1=\"abc\" attr2=\"def\"></b></a>";
final String test = "<a><b attr1=\"uvw\" attr2=\"xyz\"></b></a>";
The default ComparisonController
will let evaluate all differences:
Diff myDiff = DiffBuilder.compare(control).withTest(test)
.build();
assertThat(Linqy.count(myDiff.getDifferences()), is(2));
With the custom ComparisonController
ComparisonControllers.StopWhenDifferent
comparison is cut short once
the first difference has been encountered and only the first
difference will be returned:
Diff myDiff = DiffBuilder.compare(control).withTest(test)
.withComparisonController(ComparisonControllers.StopWhenDifferent)
.build();
assertThat(Linqy.count(myDiff.getDifferences()), is(1));
Use this if you need special configuration of the DocumentBuilderFactory
. A bug in XMLUnit 2.2.0 ignores this setting when you use ignoreWhitespace
or normalizeWhitespace
at the same time, this has been fixed in 2.2.1. ignoreComments
uses XSLT under the covers and may reduce the effect of setting the DocumentBuilderFactory
as the TransformerFactory
may need to be configured as well.