Introduction
This xml-truth
module provides a Google Truth extension to
compare XML data. It can be used as an alternative to XMLUnit. The basic
usage is as follows:
assertAbout(xml()).that(actual).hasSameContentAs(expected);
This relies on the following static imports being present:
import static com.google.common.truth.Truth.assertAbout;
import static org.apache.axiom.truth.xml.XMLTruth.xml;
actual
and expected
are objects that represent XML data. The following types are currently
supported:
-
InputStream
,Reader
,String
andbyte[]
with XML data to be parsed -
javax.xml.transform.stream.StreamSource
andorg.xml.sax.InputSource
-
DOM
Document
orElement
nodes -
java.net.URL
-
javax.xml.stream.XMLStreamReader
By default, comparison is strict. E.g. the following assertion would fail:
assertAbout(xml()).that("<p:a xmlns:p='urn:ns'/>").hasSameContentAs("<a xmlns='urn:ns'/>");
To control how the comparison is performed, use the relevant methods on the
XMLSubject
object returned by that()
:
assertAbout(xml())
.that("<p:a xmlns:p='urn:ns'/>")
.ignoringNamespacePrefixes()
.ignoringNamespaceDeclarations()
.hasSameContentAs("<a xmlns='urn:ns'/>");