How can I use JUnit to ensure that my servlets/JSP are producing valid HTML?
Created Jul 25, 2000
Tom Copeland
A better way is to use HttpUnit. HttpUnit allows the test case to be written using the same "words" as used in web pages - forms, tables, etc. You can write test cases like this (this is from the example code):
There's the traditional, brute force way - write a JUnit test case that opens a HttpURLConnection to your servlet, reads the content, and does various String.indexOf() and String.subString() operations until you're satisfied that all is well (or you're tired of hacking together String operations).
A slightly more elegant method is to use an XML parser. You can open the connection, read the contents, feed it into your XML parser, get back a document, and walk the DOM tree checking for elements which should be there. Better, but still clunky.
A better way is to use HttpUnit. HttpUnit allows the test case to be written using the same "words" as used in web pages - forms, tables, etc. You can write test cases like this (this is from the example code):
public void testWelcomePage() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(
"http://www.meterware.com/servlet/TopSecret" );
WebResponse response = conversation.getResponse( request );
WebForm forms[] = response.getForms();
assertEquals( 1, forms.length );
assertEquals( 1, forms[0].getParameterNames().length );
assertEquals( "name", forms[0].getParameterNames()[0] );
}
With a little training, test cases of this sort can be written by an HTML developer and run against the user interface on a regular basis.