2
  • Comparing two xmls using XmlUnit 2.2.1 in java.
  • The only difference between the documents is in the 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.

Rao
  • 20,781
  • 11
  • 57
  • 77

1 Answers1

3

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.

Stefan Bodewig
  • 3,260
  • 15
  • 22
  • Thank you Stefan for the quick response, that resolves the issue. – Rao Aug 10 '16 at 05:46
  • Hello Stefan, but i have some custom DifferenceEvaluator and need to ignore namesprefix what shall i do. I combine them by DifferenceEvaluators.chain(Default, new MyEvaluator("field to ignore")) - it does not work – 27P Feb 17 '20 at 10:51