tags:

views:

26

answers:

1

I have a Spring controller that returns XHTML, what's the easiest way to setup a JUnit test that verifies that the XHTML is valid? I'd also like to verify certain elements are present.

+2  A: 

The only way is to parse the output. I've used dom4j in my jUnit tests. Then you can use Xpath or DOM to extract the elements you want and test them.

If you're not already using a parser, it can take a little messing around to get it going. But once you've got it, it's very handy, and you can write all sorts of great tests. If using parsers is new to you, perhaps take a look at the dom4j quickstart guide.

John