Chapter 9. Testing and XML

 

This chapter covers

  • Comparing XML documents with XMLUnit
  • Ignoring superficial differences in XML documents
  • Testing static web pages with XMLUnit
  • Testing XSL transformations with XMLUnit
  • Validating XML documents during testing

XML documents are everywhere in J2EE: deployment descriptors, configuration files, XHTML and XSL transformations, Web Services Description Language, and on and on. If you are going to test J2EE applications, you are going to need to write tests involving XML documents. Notice the title of this chapter is “Testing and XML,” and not “Testing XML.” It is not our mission here to test XML parsers, but rather to describe how to write tests involving XML documents and other XML-related technologies, such as XSL transformations. Given that XML documents are everywhere, it looks to us that this is an important skill to have, and we can summarize the entire chapter in two words: use XPath.

The XPath language defines a way to refer to the content of an XML document with query expressions that can locate anything from single XML attribute values to large complex collections of XML elements. If you have not seen XPath before, we recommend Elizabeth Castro’s XML for the World Wide Web, which describes XML, XSL, and XPath in detail.[1] The overall strategy when testing with XML documents is to make assertions about those documents using XPath to retrieve the actual content. You want to write assertions like this:

9.1. Verify the order of elements in a document

9.2. Ignore the order of elements in an XML document

9.3. Ignore certain differences in XML documents

9.4. Get a more detailed failure message from XMLUnit

9.5. Test the content of a static web page

9.6. Test an XSL stylesheet in isolation

9.7. Validate XML documents in your tests

sitemap