Tech Ads

Back to Article List

Originally published March 2004 [ Publisher Link ]

HttpUnit : Unit Testing Web Applications

In a another article we explored how to stress test a Web application with JMeter. Use of a stress-testing tool is worthwhile when the underlying application is 100% compliant with its design objectives. In this article we will explore how to verify this compliance through another open source testing tool named HttpUnit.

Unit testing is the process by which you take a software component and evaluate its validity through a set of pre-defined tests. This mechanism assures that whatever modification you make on your code, it still at a minimum fulfills the functionality you expected of it from the outset. Unit testing is one of the main tenets in extreme programming, a software methodology in wide use today. Even if you're not into any formal software methodology, this type of testing often proves critical for software that is constantly being updated (and what software isn't!), as it uncovers unexpected bugs that creep into previously tested code.

Unit testing can be done on a wide array of components and through a series of distinct frameworks. HttpUnit in particular emulates the functionality of a browser, with the necessary provisions to perform unit tests, thus guaranteeing the integrity of Web applications.

Getting started

Before we can perform any unit tests on our Web applications, we need to load the necessary libraries into our Java CLASSPATH. These include all of the JAR files located in the jars directory of the distribution, as well as the HttpUnit classes, which are included in build/classes under the same directory structure.

The extent to which we can test the integrity of a Web application obviously depends on our particular design. For an example, we will create a test for a widely known Web application in which a minor change could have a profound impact: Google's main page.

The following HttpUnit test fetches the Google main page, analyzes its contents, and performs a series of tests to determine the total bandwidth consumed on a daily basis.

import com.meterware.httpunit.*;

public class Google {

 public static void main(String[] params ) {
   try {
    // Create a conversation for maintaining state
    WebConversation wc = new WebConversation();

    // Obtain the home page on Google
    WebRequest request = 
        new GetMethodWebRequest( "");
    WebResponse response = wc.getResponse( request );

    // Let's see how large Google's home page is
    int size = response.getContentLength();

    // Print out the page size
    System.out.println( "The Google home page 
                              is " + size + " Kb.");

    // Extract all the images
    WebImage[] imagesMainPage = response.getImages();
    int imgSizes = 0;

    // Print out the number of images in the home page
    System.out.println( "The Google home page 
      contains " + imagesMainPage.length + " images" );

     for (int i = 0; i < imagesMainPage.length; i++) {
       WebRequest imgRequest = imagesMainPage[i].getRequest();
       WebResponse imgResponse = wc.getResponse( imgRequest );
       int imgSize = imgResponse.getContentLength();
       System.out.println(" Image " + (i+1) + " 
                           is : " + imgSize + " Kb.");
	imgSizes += imgSize;

     // Total payload
     size += imgSizes;

     System.out.println("Total bandwidth for each home 
                       page visit " + size + " Kb.");

     // The daily bandwidth for Google
     // at 200 million visits a day
     double bwInMillionsKB = size * 200;

     // 1 GB to KB
     int GBToKB = 1024*1024;

     // Total bandwidth in millions of GB
     double bwInMillionsGB = bwInMillionsKB/GBToKB;

     // Total bandwidth in GB
     double totalbwInGB = bwInMillionsGB * 1000000;

     // Print out the daily bandwidth for Google's home page
     System.out.println( "Total daily bandwidth at 
       200 million visits : " + totalbwInGB + " GB" );

      } catch (Exception e) {
         System.err.println( "Exception: " + e );

Our first step is to create a WebConversation object. This class is the foundation for HttpUnit, since it provides the main emulation capabilities of a browser. We then create a WebRequest on the Google main page, which is then delegated to a WebResponse object for further analysis.

Once we have the response, we invoke the getContentLength method to get the initial payload on the main page. Immediately after, we inspect how many images are on the page, since these also influence the total bandwidth per visit. The images are later extracted with the getImages method and assigned to a WebImage array. Later we perform an iteration on each of its elements, inspecting the size of each image. We finally compound the total payload for the main page by the approximate daily visits and obtain a 24-hour bandwidth usage stat.

As you may realize from the numbers we obtain on this HttpUnit example, the importance of this type of testing hinges on the impact a minor modification can have on an application's traffic. Fortunately, HttpUnit is such a versatile tool that it allows you to perform unit tests on a wide base of areas, ranging from HTML menus, cookie simulation, JavaScript, and practically every other capability offered by a browser.

Use HttpUnit to create specific unit tests for all your Web applications to be sure that on every occasion you make modifications to your code your initial design objectives hold true.

Originally published March 2004 [ Publisher Link ]

Back to Article List