0

I have tried the below example and tried with different methods of DiffBuilder and CompareMatcher classes:

import org.junit.jupiter.api.Test;
import org.xmlunit.diff.DefaultNodeMatcher;
import org.xmlunit.diff.ElementSelectors;
import org.xmlunit.matchers.CompareMatcher;
import static org.hamcrest.MatcherAssert.assertThat;

public class XmlDemo4 {

    @Test
    public void demoMethod() {
    String actual = "<struct><int>3</int><boolean>false</boolean></struct>";
    String expected = "<struct><boolean>false</boolean><int>4</int></struct>";

    assertThat(actual, CompareMatcher.isSimilarTo(expected)
            .ignoreWhitespace().normalizeWhitespace().
            withNodeMatcher(new 
    DefaultNodeMatcher(ElementSelectors.byName,ElementSelectors.Default)));

    }

}

by running above code I am getting:

java.lang.AssertionError: 
Expected: Expected text value '4' but was '3' - comparing <int ...>4</int> at 
/struct[1]/int[1]/text()[1] to <int ...>3</int> at /struct[1]/int[1]/text()[1]:
<int>4</int>
     but: result was: 
<int>3</int>

at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
at StringXml.XmlDemo4.demoMethod(XmlDemo4.java:29)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)

Process finished with exit code -1

here it is comparing content text also, please suggest, is there any way to compare the content text with the data type and it should not compare with the exact content text

Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
KB1
  • 51
  • 5

2 Answers2

0

You need some implementation of XML diff. I am not aware of Java, but there is some python library: https://pypi.org/project/xmldiff/

See also Are there any free Xml Diff/Merge tools available?

Queeg
  • 7,748
  • 1
  • 16
  • 42
0

DifferenceEvaluator is the class which decides whether a difference is SIMILAR, EQUAL or DIFFERENT. In my approach I am saying if there is any comparison of type TEXT then make the result of those comparison as SIMILAR Refer to XMLUnit 2.x

import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.diff.*;

import static org.xmlunit.diff.ComparisonType.TEXT_VALUE;

public class XmlDemo {

public static void main(String[] args) {
    String actual = "<struct><int>6</int><boolean>false</boolean></struct>";
    String expected = "<struct><boolean>false</boolean><int>4</int></struct>";

    Diff diff = DiffBuilder.compare(actual)
            .checkForSimilar()
            .withTest(expected)
            .ignoreWhitespace().normalizeWhitespace()
            .withNodeMatcher(new
                    DefaultNodeMatcher(ElementSelectors.byName))
            .withDifferenceEvaluator(DifferenceEvaluators.chain(DifferenceEvaluators.Default, DifferenceEvaluators.downgradeDifferencesToSimilar(TEXT_VALUE)))
            .build();

    for (Difference difference: diff.getDifferences())
        System.out.println(difference);
}

}

Varshaa
  • 1
  • 1