我原来的答案已经过时了。如果必须再次构建它,我将使用xmlunit 2和xmlunit-
matchers。请注意,对于xml单元,不同的顺序始终是“相似”而不是相等。
@Testpublic void testXmlUnit() { String myControlXML = "<test><elem>a</elem><elem>b</elem></test>"; String expected = "<test><elem>b</elem><elem>a</elem></test>"; 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)));}如果有人仍然不想使用纯Java实现,那就是。此实现从xml中提取内容,并比较列表的忽略顺序。
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);}@Testpublic void test() throws Exception { document doc = loadXMLFromString("<test>n" + " <elem>b</elem>n" + " <elem>a</elem>n" + "</test>"); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathexpression expr = xpath.compile("//test//elem"); NodeList all = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); List<String> values = new ArrayList<>(); if (all != null && all.getLength() > 0) { for (int i = 0; i < all.getLength(); i++) { values.add(all.item(i).getTextContent()); } } Set<String> expected = new HashSet<>(Arrays.asList("a", "b")); assertThat("List equality without order", values, containsInAnyOrder(expected.toArray()));}


