2

I have two xml files. First contains namespace prefix and the second one doesn't. So when I assert them I want to ignore these prefixes and check for equality.

 @Test
    public void test1(){
        String control = "<prefix1:element xmlns:prefix1=\"namespace\" >Some text</prefix1:element>";
        String test = "<element xlmns=\"namespace\">Some text</element>";

        XmlAssert.assertThat(controlElement).and(testElement)
                .ignoreChildNodesOrder()
                .ignoreComments()
                .ignoreWhitespace()
                .withNodeMatcher(new DefaultNodeMatcher((ElementSelector) (control,test)->
                {
                return control!=null && test !=null && Objects.equals(control.getLocalName(),test.getLocalName());
                }
                ))
                .withDifferenceEvaluator((comparison,outcome)->{
                    System.out.println(comparison);
                    System.out.println(outcome);
                    if (outcome == ComparisonResult.EQUAL){
                        return outcome;
                    }
                    if (comparison.getType() == ComparisonType.NAMESPACE_URI
                    || comparison.getType()==ComparisonType.NAMESPACE_PREFIX){
                        System.out.println("aaaa");
                        return ComparisonResult.SIMILAR;
                    }
                    return outcome;
                })
                .areSimilar();
    }

So, this test is not passing and I get the following message:

java.lang.AssertionError: 
Expecting:
 <<prefix1:element xmlns:prefix1="namespace">Some text</prefix1:element>>
to be equal to:
 <<element xlmns="namespace">Some text</element>>
but was not.

And what I need is that - this test case should be passing because all of the nodes and values are equal. Basically, a way to ignore the prefix

fcka
  • 21
  • 2
  • 1
    there is a typo in `xmlns` for the element using the default namespace. After fixing that (and renaming the Strings to `controlElement` and `testElement`) your test passes. – Stefan Bodewig Mar 26 '21 at 09:11

0 Answers0