Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My original answer is outdated. If I would have to build it again i would use xmlunit 2 and xmlunit-matchers. Please note that for xml unit a different order is always 'similar' not equals.</p> <pre><code>@Test public void testXmlUnit() { String myControlXML = "&lt;test&gt;&lt;elem&gt;a&lt;/elem&gt;&lt;elem&gt;b&lt;/elem&gt;&lt;/test&gt;"; String expected = "&lt;test&gt;&lt;elem&gt;b&lt;/elem&gt;&lt;elem&gt;a&lt;/elem&gt;&lt;/test&gt;"; assertThat(myControlXML, isSimilarTo(expected).withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))); //In case you wan't to ignore whitespaces add ignoreWhitespace().normalizeWhitespace() assertThat(myControlXML, isSimilarTo(expected).ignoreWhitespace().normalizeWhitespace().withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))); } </code></pre> <p>If somebody still want't to use a pure java implementation here it is. This implementation extracts the content from xml and compares the list ignoring order.</p> <pre><code>public static Document loadXMLFromString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); return builder.parse(is); } @Test public void test() throws Exception { Document doc = loadXMLFromString("&lt;test&gt;\n" + " &lt;elem&gt;b&lt;/elem&gt;\n" + " &lt;elem&gt;a&lt;/elem&gt;\n" + "&lt;/test&gt;"); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile("//test//elem"); NodeList all = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); List&lt;String&gt; values = new ArrayList&lt;&gt;(); if (all != null &amp;&amp; all.getLength() &gt; 0) { for (int i = 0; i &lt; all.getLength(); i++) { values.add(all.item(i).getTextContent()); } } Set&lt;String&gt; expected = new HashSet&lt;&gt;(Arrays.asList("a", "b")); assertThat("List equality without order", values, containsInAnyOrder(expected.toArray())); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload