Share on Google+Share on Google+

Unit Testing with HTTPUnit

Learn how to perform Unit Testing with HTTPUnit.

Unit Testing with HTTPUnit

This tutorial shows you how to use HttpUnit to test your web applications. HttpUnit enables the programmers to write the test script to test web applications.

What is HttpUnit?

HttpUnit is Java based free, Open Source Unit testing tool to test the web application. This tool is based on Unit which is also Java based free testing tool. HttpUnit emulates the functionality of the web browser, including the form submission, http authentication, cookies, Java Scripts and the Java API to examine the returned page.

Architecture of HttpUnit

HttpUnit consists of two core components:

  • Web Client: Web client part of the HttpUnit is responsible for sending and receiving the responses from and to the web/application server.
  • API to Analyze the the response content: This component is responsible acting on the response contents.

Downloading HttpUnit

HttpUnit is freely available on the and can be downloaded from

Installing HttpUnit

To use the HttpUnit your system should have  JDK 1.3 or higher. Extract the downloaded file into your favorite directory. Create learnhttpunit at your favorite place and create lib directory in it. To use HttpUnit put httpunit.jar and Tidy.jar in class path. For my example I have created lib directory under learnhttpunit directory and copied the content of lib and jars directory into it. I have created a bat file addtoppath.bat, which needs to be run from the command to add library files into CLASSPATH. My addtopath.bat files looks like:

set CLASSPATH=.;%CLASSPATH%;lib/httpunit.jar;lib/js.jar;lib/junit.jar;lib/nekohtml.jar;lib/servlet.jar;lib/Tidy.jar;lib/xercesImpl.jar;lib/xmlParserAPIs.jar

Now I will show you how to create your first program use HttpUnit to access the content of a jsp file.

Accessing the content of JSP file using HttpUnit

I this example we will develop a program to access the JSP file from tomcat server running locally. 


<title>I am Example 1</title>
<p>Example 1: Welcome to the example 1.</p>

Create a JSP file file example1.jsp with above content in your webapps/ROOT directory of your tomcat and run the tomcat. Open web browser and type http://localhost:8080/example1.jsp to test example1.jsp.

Now create and put in learnhttpunit directory.

import com.meterware.httpunit.*;

//This example reads the example1.jsp from web server 
and displays its title and content.

public class example1 {

public static void main( String[] params ) {
try {
  // create the conversation object 
  // to maintain state
  WebConversation wc = new WebConversation();

  // Obtain the example1.jsp
  WebRequest request = new 
GetMethodWebRequest( "http://localhost:8080/example1.jsp" );
   WebResponse response = wc.getResponse( request );

  //Display the title
  System.out.println("Title is :" + response.getTitle() );
  //Display the content of the page
  System.out.println("Page content is :" + response.getText() );

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


To compile and test the example go to learnhttpunit directory and execute addtopath.bat to place jar files in CLASSPATH. Compile and run it. You should see the following output.

C:\learnhttpunit>java example1
Title is :I am Example 1
Page content is :<html>
<title>I am Example 1</title>
<p>Example 1: Welcome to the example 1.</p>

Now lets examine the code. 

import com.meterware.httpunit.*

is the httpunit library import.

In order to maintain the conversation state( Cookies, Session, Windows ) with the web server WebConversation class it used. First of all it is necessary to create an object of this class to maintain the state with the server and play the role of web browser.

WebConversation wc = new WebConversation();

The GetMethodWebRequest(String url) creates an HTTP request using the GET method.

WebRequest request = new GetMethodWebRequest( "http://localhost:8080/example1.jsp" );

Processing the Content of TABLE

Now I will show you how to read the content of table using HttpUnit API. For this create another jsp file (example2.jsp) and put in the webapps/ROOT of your tomcat.



Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.