Class CompareMatcher
- All Implemented Interfaces:
org.hamcrest.Matcher<Object>,org.hamcrest.SelfDescribing,DifferenceEngineConfigurer<CompareMatcher>
Matcher compares two XML sources with each others.
The Test-Object and Control-Object can be all types of input supported by Input.from(Object).
Simple Example
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));
Complex Example
In some cases you may have a static factory method for your project which wraps all project-specific configurations
like customized ElementSelector or DifferenceEvaluator.
public static CompareMatcher isMyProjSimilarTo(final File file) {
return CompareMatcher.isSimilarTo(file)
.throwComparisonFailure()
.normalizeWhitespace()
.ignoreComments()
.withNodeMatcher(new DefaultNodeMatcher(new MyElementSelector()))
.withDifferenceEvaluator(DifferenceEvaluators.chain(
DifferenceEvaluators.Default, new MyDifferenceEvaluator()));
}
And then somewhere in your Tests:
assertThat(test, isMyProjSimilarTo(controlFile));
-
Method Summary
Modifier and TypeMethodDescriptionvoiddescribeMismatch(Object item, org.hamcrest.Description description) voiddescribeTo(org.hamcrest.Description description) ignoreCommentsUsingXSLTVersion(String xsltVersion) static CompareMatcherisIdenticalTo(Object control) Create aCompareMatcherwhich compares the test-Object with the given control Object for identity.static CompareMatcherisSimilarTo(Object control) Create aCompareMatcherwhich compares the test-Object with the given control Object for similarity.booleanInstead of Matcher returningfalseaorg.junit.ComparisonFailurewill be thrown.withAttributeFilter(Predicate<Attr> attributeFilter) withComparisonController(ComparisonController comparisonController) Throws an exception as you theComparisonControlleris completely determined by the factory method used.withComparisonFormatter(ComparisonFormatter comparisonFormatter) Use a custom Formatter for the Error Messages.withComparisonListeners(ComparisonListener... comparisonListeners) withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator) withDifferenceListeners(ComparisonListener... comparisonListeners) withNamespaceContext(Map<String, String> prefix2Uri) withNodeFilter(Predicate<Node> nodeFilter) withNodeMatcher(NodeMatcher nodeMatcher) Methods inherited from class org.hamcrest.BaseMatcher
_dont_implement_Matcher___instead_extend_BaseMatcher_, toString
-
Method Details
-
isIdenticalTo
Create aCompareMatcherwhich compares the test-Object with the given control Object for identity.As input all types are supported which are supported by
Input.from(Object).- Parameters:
control- the control XML source- Returns:
- a CompareMatcher
-
isSimilarTo
Create aCompareMatcherwhich compares the test-Object with the given control Object for similarity.Example for Similar: The XML node "<a>Text</a>" and "<a><![CDATA[Text]]></a>" are similar and the Test will not fail.
The rating, if a node is similar, will be done by the
DifferenceEvaluators.Default. SeeDiffBuilder.withDifferenceEvaluator(DifferenceEvaluator)As input all types are supported which are supported by
Input.from(Object).- Parameters:
control- the control XML source- Returns:
- a CompareMatcher
-
ignoreWhitespace
- Returns:
- this
- See Also:
-
normalizeWhitespace
- Returns:
- this
- See Also:
-
ignoreComments
- Returns:
- this
- See Also:
-
ignoreElementContentWhitespace
- Returns:
- this
- Since:
- XMLUnit 2.6.0
- See Also:
-
ignoreCommentsUsingXSLTVersion
- Parameters:
xsltVersion- use this version for the stylesheet- Returns:
- this
- Since:
- XMLUnit 2.5.0
- See Also:
-
withNodeMatcher
- Specified by:
withNodeMatcherin interfaceDifferenceEngineConfigurer<CompareMatcher>- See Also:
-
withDifferenceEvaluator
- Specified by:
withDifferenceEvaluatorin interfaceDifferenceEngineConfigurer<CompareMatcher>- See Also:
-
withComparisonListeners
- Specified by:
withComparisonListenersin interfaceDifferenceEngineConfigurer<CompareMatcher>- See Also:
-
withDifferenceListeners
- Specified by:
withDifferenceListenersin interfaceDifferenceEngineConfigurer<CompareMatcher>- See Also:
-
withNamespaceContext
- Specified by:
withNamespaceContextin interfaceDifferenceEngineConfigurer<CompareMatcher>- Since:
- XMLUnit 2.1.0
- See Also:
-
withAttributeFilter
- Specified by:
withAttributeFilterin interfaceDifferenceEngineConfigurer<CompareMatcher>- See Also:
-
withNodeFilter
- Specified by:
withNodeFilterin interfaceDifferenceEngineConfigurer<CompareMatcher>- See Also:
-
throwComparisonFailure
Instead of Matcher returningfalseaorg.junit.ComparisonFailurewill 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 inorg.junit.Assert#assertEquals(Object, Object)if both values areStrings.The only disadvantage is, that you can't combine the
CompareMatcherwith other Matchers (likeCoreMatchers.not(Object)) anymore. The following code will NOT WORK properly:assertThat(test, not(isSimilarTo(control).throwComparisonFailure()))- Returns:
- this
-
withComparisonFormatter
Use a custom Formatter for the Error Messages. The defaultFormatter isDefaultComparisonFormatter.- Specified by:
withComparisonFormatterin interfaceDifferenceEngineConfigurer<CompareMatcher>
-
withDocumentBuilderFactory
- Parameters:
f- the DocumentBuilderFactory to use- Returns:
- this
- Since:
- XMLUnit 2.2.0
- See Also:
-
withComparisonController
Throws an exception as you theComparisonControlleris completely determined by the factory method used.- Specified by:
withComparisonControllerin interfaceDifferenceEngineConfigurer<CompareMatcher>- Since:
- XMLUnit 2.6.0
-
matches
- Specified by:
matchesin interfaceorg.hamcrest.Matcher<Object>
-
describeTo
public void describeTo(org.hamcrest.Description description) - Specified by:
describeToin interfaceorg.hamcrest.SelfDescribing
-
describeMismatch
-