Z XMLUnit 2 jak porównać dwa dokumenty bez uwzględnienia kolejności elementów?Porównaj element ignorujący element XML
Mam this question for XMLUnit 1, ale najwyraźniej nowy interfejs API w wersji 2 nie ma już wspomnianej metody.
To jest mój bieżący kod:
Diff diff = DiffBuilder.compare(expected)
.withTest(actual)
.ignoreComments()
.ignoreWhitespace()
.checkForSimilar()
.build();
assertFalse(diff.hasDifferences());
Edit Stefan Bodewigs komentarza:
Są dwa ciągi i porównać z powyższym fragmencie:
String expected = "<root><foo>FOO</foo><bar>BAR</bar></root>";
String actual = "<root><bar>BAR</bar><foo>FOO</foo></root>";
Podawane dyferencjału
Expected element tag name 'foo' but was 'bar' - comparing <foo...> at /root[1]/foo[1] to <bar...> at /root[1]/bar[1] (DIFFERENT)
Expected text value 'FOO' but was 'BAR' - comparing <foo ...>FOO</foo> at /root[1]/foo[1]/text()[1] to <bar ...>BAR</bar> at /root[1]/bar[1]/text()[1] (DIFFERENT)
Expected element tag name 'bar' but was 'foo' - comparing <bar...> at /root[1]/bar[1] to <foo...> at /root[1]/foo[1] (DIFFERENT)
Expected text value 'BAR' but was 'FOO' - comparing <bar ...>BAR</bar> at /root[1]/bar[1]/text()[1] to <foo ...>FOO</foo> at /root[1]/foo[1]/text()[1] (DIFFERENT)
Nowe API powinien ignorować elementu kolejność domyślnie (to właśnie 'DifferenceEvaluators.DEFAULT' robi). Czy na pewno jest to różnica, którą widzisz, a nie coś innego? –
Powieliłem problem z OP, nawet gdy dodałem .checkForSimilar(). Wydaje się to być sprzeczne z dokumentacją na https://github.com/xmlunit/user-guide/wiki/DifferenceEvaluator#default-differenceevaluator Dodanie opisanej poniżej metody .withNodeMatcher() rozwiązało problem. –