
Simulated JUnit tests for Web Applications using HTTPUnit
23 01 2009HttpUnit is a suite of Java classes to test Web applications over HTTP. Coupled with JUnit, HttpUnit is a powerful tool for creating test suites to ensure the end-to-end functionality of your Web applications.
While JUnit itself can test server-side Java code by writing test harnesses for individual classes, HttpUnit extends JUnit to test the integration over the Web by emulating the way a Web browser works and working through a Web server. This article will explain how you can take advantage of HttpUnit.
One great aspect of HttpUnit is that it can test entire Web applications, not just single pages. Because it honors form information and cookies, you can write tests to cover a whole session. For example, if your Web application includes a shopping cart, you could write a test to try logging in, selecting an item, placing it the shopping cart, and checking out. Since the tests are written in Java, there’s no limit to how in-depth your tests can be. Let’s get started by looking at a simple HttpUnit test.
Making a request
Since HttpUnit can emulate an entire session and not just a single request, the system uses a class, called WebConversation, to manage the requests, handle the cookies, and resolve relative URLs. As you write more complicated tests, the WebConversation class will become more important. Usually, the class simply creates a request to the URL base_url. The response from this request is stored, and the JUnit method assertEquals() tests whether the response was 200, which is the HTTP response for a successful request.
Parsing a response
Once you’ve made a successful request to a Web server, it’s time to parse the result of the request. HttpUnit makes use of the JTidy package, which is included in the HttpUnit distribution, to parse the resulting HTML into a Document Object Model (DOM) tree. For those who aren’t familiar with DOM trees, they offer a uniform way to manipulate a document in a hierarchical data structure. JTidy provides a standardized way to manipulate the HTML result.
You should note, however, that the DOM tree that JTidy builds represents the structure that a document should have, not necessarily what it does have. That means that JTidy may add structural elements to the document tree that aren’t in the HTML source but should be. That includes head and body markers, paragraph marks, font tags, and more. Iterating through a DOM tree and visualizing the output, can be helpful.
Shortcuts through the DOM
Navigating through the DOM can be difficult and time-consuming. Fortunately, HttpUnit includes tools to make quick work of dealing with some HTML elements.
Navigating links
HttpUnit offers much more than just parsing the result of a single connection. The real power comes from being able to make multiple requests through the WebConnection object. The easiest way to make multiple requests is by following HTML links.
Comments : 1 Comment »
Categories : Software

