Rao
Rao

Reputation: 21379

XmlUnit 2.x compare 2 xmls fail because of difference in namespace prefix

Here is code snippet to reproduce:

 @Test 
 public void testDifferentPrefix() {
            final String control = "<ns:a xmlns:ns='abc'><b attr=\"xyz\"></b></ns:a>";
            final String test = "<ns1:a xmlns:ns1='abc'><b attr=\"xyz\"></b></ns1:a>";

            Diff myDiff = DiffBuilder.compare(Input.fromString(control))
                              .withTest(Input.fromString(test))
                              .build();
            Assert.assertFalse(myDiff.toString(), myDiff.hasDifferences());        
}

when the above test is run, it is failing with below error:

Expected namespace prefix 'ns' but was 'ns1' - comparing at /pfx:a[1] to at /pfx:a[1] junit.framework.AssertionFailedError at NewEmptyJUnitTest.testDifferentPrefix(NewEmptyJUnitTest.java:95)

What should be corrected in order to avoid the error? I believe that am missing something trivial.

Upvotes: 2

Views: 3744

Answers (1)

user4524982
user4524982

Reputation:

By not specifying any DifferenceEvaluator you are implicitly using DifferenceBuilders.DEFAULT for which a different namespace prefixes are "SIMILAR" differences.

If you want to ignore "SIMILAR" differences you must set checkForSimilar() on the DiffBuilder.

Upvotes: 3

Related Questions