Class CompareMatcher

  • All Implemented Interfaces:
    org.hamcrest.Matcher<java.lang.Object>, org.hamcrest.SelfDescribing, DifferenceEngineConfigurer<CompareMatcher>

    public final class CompareMatcher
    extends org.hamcrest.BaseMatcher<java.lang.Object>
    implements DifferenceEngineConfigurer<CompareMatcher>
    This Hamcrest 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));