-
Notifications
You must be signed in to change notification settings - Fork 8
CompareMatcher
XMLUnit 2.x has not been released, yet. This documentation may change until the final release of XMLUnit. |
---|
The Hamcrest CompareMatcher
compares two XML sources with each others.
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>";
assertThat(test, CompareMatcher.isIdenticalTo(control));
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 Difference between identical and similar is decided by the default DifferenceEvaluator.
The compare Matcher can be configured via fluent API:
CompareMatcher.isSimilarTo(control)
.ignoreComments() // [1]
.ignoreWhitespace() // [2]
.normalizeWhitespace() // [3]
.throwComparisonFailure() // [4]
.withComparisonFormatter(comparisonFormatter) // [5]
.withComparisonListeners(comparisonListeners) // [6]
.withDifferenceEvaluator(differenceEvaluator) // [7]
.withDifferenceListeners(comparisonListeners) // [8]
.withNodeMatcher(nodeMatcher); // [9]
-
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. -
throwComparisonFailure():
This will throw a org.junit.ComparisonFailure in case of differences. Eclipse, IntelliJ and NetBeans can than show you a nice Diff-View of the differences.
See throwComparisonFailure. -
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.
Instead of Matcher returning false a org.junit.ComparisonFailure
will be thrown.
The advantage over the standard Matcher behavior is, that the ComparisonFailure can provide the effected Control-Node and Test-Node in separate Properties.
Eclipse, NetBeans and IntelliJ can provide a nice DIFF-View for the two values.
ComparisonFailure is also used in org.junit.Assert.assertEquals(Object, Object)
if both values are Strings:
The only disadvantage is, that you can't combine the CompareMatcher with other Matchers (like org.hamcrest.CoreMatchers.not(Object))
anymore. The following code will NOT WORK properly: assertThat(test, not(isSimilarTo(control).throwComparisonFailure()))
- Overview
- General Concepts
- Comparing XML
- Validating XML
- Utilities
- Migrating from XMLUnit 1.x to 2.x
- Known Issues