XMLUnit

XMLUnit provides extensions to the JUnit framework to allow assertions to be made about XML content.

### Using XMLUnit

1. Create a subclass of {@link org.custommonkey.xmlunit.XMLTestCase XMLTestCase} something like this:

       public class TestSomething extends XMLTestcase {
           // standard JUnit style constructor
           public TestSomething(String name) {
               super(name);
           }
           // standard JUnit style method
           public static TestSuite suite() {
               return new TestSuite(TestSomething.class);
           }
       }

2. Set the global JAXP settings in {@link org.custommonkey.xmlunit.XMLUnit XMLUnit} so that your chosen parser and transformer are used for the tests.
   Note:You can skip this bit if you use the default JAXP settings or you have an Ant task that uses -D JVM options to specify the JAXP settings.

           // set the JAXP factories to use the Xerces parser
           // - declare to throw Exception as if this fails then all the tests will
           // fail, and JUnit copes with these Exceptions for us
           public void setUp() throws Exception {
               XMLUnit.setControlParser(
                   "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
               // this next line is strictly not required - if no test parser is
               // explicitly specified then the same factory class will be used for
               // both test and control
               XMLUnit.setTestParser(
                   "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

               XMLUnit.setSAXParserFactory(
                   "org.apache.xerces.jaxp.SAXParserFactoryImpl");
               XMLUnit.setTransformerFactory(
                   "org.apache.xalan.processor.TransformerFactoryImpl");
           }

3. Add test methods to make your assertions: the {@link org.custommonkey.xmlunit.XMLTestCase XMLTestCase} javadoc lists the available assertion methods and their usage, but here are some examples...

           public void testObjectAsXML() throws Exception {
               String expectedXML = "....";
               String objectAsXML = null;
               //...set up some object here and serialize its state into
               //our test String...
               assertXMLEqual(expectedXML, objectAsXML);
           }

           public void testTransformToFormatB() throws Exception {
               String expectedFormatB = "....";
               String formatA = "....";
               String transformXSLT = "....";
               Transform formatAToFormatB = new Transform(formatA, transformXSLT);
               assertXMLEqual(new Diff(expectedFormatB, formatAToFormatB), true);
           }

           public void testIsValidAfterTransform() throws Exception {
               String incomingMessage = "....";
               String toSourceSystemXSLT = "....";
               Transform transform = new Transform(incomingMessage, toSourceSystemXSLT);
               assertXMLValid(transform.getResultString());
           }

           public void testXpaths() throws Exception {
               String ukCustomerContactPhoneNos = "//customer[@country='UK']/contact/phone";
               String customerExtract1 = "....";
               String customerExtract2 = "....";
               assertXpathsNotEqual(ukCustomerContactPhoneNos, customerExtract1,
                   ukCustomerContactPhoneNos, customerExtract2);
           }

           public void testXpathValues() throws Exception {
               String firstListItem = "/html/body/div[@id='myList']/h1/ol/li[1]";
               String secondListItem = "/html/body/div[@id='myList']/h1/ol/li[2]";
               String myHtmlPage = "....";
               assertXpathValuesNotEqual(firstListItem, secondListItem, myHtmlPage);
           }

           public void testSpecificXpath() throws Exception {
               String todaysTop10 = "count(//single[@topTen='true'])";
               String playlist = "....";
               assertXpathEvaluatesTo("10", todaysTop10, playlist);

           }

### A little bit of history

XMLUnit is the result of the efforts of Tim Bacon and Jeff Martin. We needed a tool to test the serialization and de-serialiation of objects into XML for an application that sent and received XML messages over HTTP, and this project grew out of that. We hope you find it useful, and welcome any feedback you can give us.