March 7, 2021
Hot Topics:

Extreme Java GUI Testing

  • By Thomas Hammell
  • Send Email »
  • More Articles »


Testing Web interfaces is also difficult to do with the normal JUnit classes. HTTPUnit solves this problem by providing a set of classes that emulate Web browser behavior, including form submission, basic http authentication, cookies, and automatic page redirection. The main focus of HTTPUnit is the WebConversation class. The idea is that this class provides a way for JUnit test methods to have a conversation with a Web site. The other main classes are WebRequest, which is used to create an HTTP request, and WebResponse, which is used to store the results of a request.

HTTPUnit also provides classes that allow test methods to view the WebResponse in a number of ways. A WebResponse can be viewed by:

  • Getting the actual input stream of the response and parsing through it.
  • Converting the return HTML into an XML DOM and then parsing and searching it as an XML document.
  • The HTML forms, tables, and links can be viewed as a collection of HTTPUnit WebForms, WebTables, and WebLinks.

Let's look at a simple Web application and HTTPUnit. This application is a simple airline reservation system that shows a user the flights that he has booked and allows him to book additional flights. There are three Web pages. The first page is a login screen, where the user enters his name. The second screen shows the list of flights that the user presently has reservations on. These screens are shown in Figures 2 and 3 below. The third screen shows a list of available flights given a set of airports and date of flight. It is show in Figure 4 for a flight from San Francisco to Newark on 3/30.

Figure 2

Figure 3

Figure 4

If you were to test this application manually, you would go the login screen, enter a user name, then verify that the flights listed were correct for that user. Then you would try to add a new flight for that user.

Let's see how this could be done automatically using HTTPUnit. (The full set of source for all applications and unit test can be downloaded at

The testWelcomePage method creates and new WebConversion, then issues the request to the actual Web page by creating a new WebRequest. The conversation is then used to get the response from the request.

To learn more about JUnit extensions, check out the extension link on the JUnit site (see references).

Tips for Using HTTPUnit and JFCUnit Effectively

Unit testing user interfaces is by its nature different than most other forms of unit testing. Most unit tests are written to exercise the inner methods of a class. Unit testing user interfaces must be treated more like black-box testing, because the unit test is testing the interface with the user, not the internal methods of the actual class presenting the interface. For this reason, it is important that before you actually start writing unit tests for a user interface that you try to create a test plan that identifies how the user is expected to interact with the system. This will allow you to better understand what unit tests need to be created in order to effectively test the user interface.

JFCUnit and HTTPUnit should only be used to test the GUI components of the system. Most user interfaces contain a number of other classes, like table models and other utility classes, that have no GUI components and can be tested using the normal JUnit classes. Normal JUnit test should be used as much as possible, because they more thoroughly test the actual class.

HTTPUnit does have one serious limitation. It can't run any JavaScript code contained in a Web page. This is a known limitation of HTTPUnit, and it is hoped that a future release of HTTPUnit may contain this feature.

Although HTTPUnit is a fully released and widely used API, JFCUnit is still in beta release, although very useful in its present form. It is still changing and does not have the support of a larger user community. I have been using it for the past four months and have found it useful.

There is an additional Swing testing tool that was released by Sun recently. It is called Jemmy and is similar to JFCUnit. It doesn't fit as neatly into the JUnit framework as JFCUnit does, but it is a little more mature than JFCUnit and has a number of nice features that make it worth considering when looking for a tool to automate GUI testing.

Even with these limitations, JFCUnit and HTTPUnit are indeed very useful tools for developers who've adopted extreme programming practices to automate user interface testing. So the next time you are manually testing a GUI, possibly for the " P < spent. well time be will It task. the automate to taking consider time, umpteenth?>


About the Author

Thomas Hammell is a senior developer at Hewlett-Packard and is part of the Telecom Infrastructure Group, which develops various tools used to Web-enable telecom applications. He has over 17 years of experience in developing software. Hammell has published a number of articles on Java topics ranging from Swing development to Unit testing and presented a session, Extreme GUI Testing, at the JavaOne Conference in March, 2002. Tom holds a Bachelor of Science in Electrical Engineering and Masters of Computer Science from Stevens Institute of Technology.

      Page 2 of 2

      This article was originally published on April 26, 2002

      Enterprise Development Update

      Don't miss an article. Subscribe to our newsletter below.

      Thanks for your registration, follow us on our social networks to keep up-to-date