Learn how to perform Unit Testing with HTTPUnit.
Learn how to perform 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:
Downloading HttpUnit
HttpUnit is freely available on the sourceforge.net and can be downloaded from http://sourceforge.net/project/showfiles.php?group_id=6550.
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.
example1.jsp
<html> <head> <title>I am Example 1</title> </head> <body> <p>Example 1: Welcome to the example 1.</p> </body> </html> |
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 example1.java 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 {
|
To compile and test the example go to learnhttpunit directory and execute addtopath.bat to place jar files in CLASSPATH. Compile example1.java and run it. You should see the following output.
C:\learnhttpunit>java example1 Title is :I am Example 1 Page content is :<html> <head> <title>I am Example 1</title> </head> <body> <p>Example 1: Welcome to the example 1.</p> </body> </html> |
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.