Written by Olivier Le Diouris, Oracle Corporation
February 2004.
Popularized by the eXtreme Programming, JUnit allows you write the unit tests for your Java code.
From Java as well, but to test application running over the internet, HttpUnit intends to achieve the same
kind of goals.
We are going to show how to test an application running over the internet.
It's quite interesting to see that the tests will be written in Java, using the classes provided by HttpUnit, and run
by JUnit. The application itself does not have to be written in Java, we will see that it could be static pages, or
dynamic ones. HttpUnit focuses on the existence of data, and their possible content.
The application we are going to be able to test could run in a J2EE container, use ADF, or even ms.net
another server.
Create the HttpUnit library
Once you've downloaded an unzipped the HttpUnit distribution, you have to create a library out of its components.
We suggest you call this library HttpUnit Runtime, we will refer to it through this name
later in this document. It must at least contain the following jar-files:
httpunit-1.5.5-d-19Sep\jars\js.jar
httpunit-1.5.5-d-19Sep\jars\Tidy.jar
httpunit-1.5.5-d-19Sep\lib\httpunit.jar
Basic test
Create infrastructure
Create a new workspace in JDeveloper, and create a new empty project in it. Associate with this new project the library you've just created,
called HttpUnit Runtime. Also add the JUnit runtime library. This library is created by the JUnit
extension. If it is missing, it means that your JUnit extension is not installed properly.
Create a Test Case
Create a new class in the package of your choice, and make sure you extend junit.framework.TestCase.
This first example intends to test the existence of a specific resource somewhere on the net.
Write a code like this one:
package httpunit;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import junit.framework.TestCase;
/**
* Check the existence of a page
*/
public class TestJumpJDev extends TestCase
{
public TestJumpJDev(String name)
{
super(name);
}
public void testJumpJDevPageExists() throws Exception
{
WebConversation webConversation = new WebConversation();
WebRequest request = new GetMethodWebRequest("http://jumpjdev.sourceforge.net");
WebResponse response = webConversation.getResponse(request);
}
}
Create a Test Suite
Once the Test Case is written and compiled, you need to build a Test Suite to run it.
This has no specificity because of HttpUnit, it's a regular JUnit one, and the JUnit Wizard will help you
to create it.
package httpunit.test;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllHttpUnitTests
{
public static Test suite()
{
TestSuite suite;
suite = new TestSuite("AllHttpUnitTests");
suite.addTestSuite(httpunit.TestJumpJDev.class);
return suite;
}
public static void main(String args[])
{
String args2[] = {"-noloading", "httpunit.test.AllHttpUnitTests"};
junit.swingui.TestRunner.main(args2);
}
}
Run the Test Suite
The Test Suite is now ready to run. Just make sure your proxy is set correctly in case the URL you whish
to reach is behind the firewall... (Tools | Preferences | Web Browser and Proxy)
Follow hyperlinks
Now we can reach the page we're interested in, we want to simulate the navigation behavior of a user.
Let us write another Test Case:
package httpunit;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import junit.framework.TestCase;
/**
* We want to follow hyperlinks
*/
public class TestJumpJDevTwo extends TestCase
{
public TestJumpJDevTwo(String name)
{
super(name);
}
public void testJumpJDevUsageExists() throws Exception
{
WebConversation webConversation = new WebConversation();
WebRequest request = new GetMethodWebRequest("http://jumpjdev.sourceforge.net");
WebResponse response = webConversation.getResponse(request);
WebLink usageLink = response.getLinkWith("Usage");
WebRequest clickRequest = usageLink.getRequest();
WebResponse usage = webConversation.getResponse(clickRequest);
}
public void testJumpJDevInstallExists() throws Exception
{
WebConversation webConversation = new WebConversation();
WebRequest request = new GetMethodWebRequest("http://jumpjdev.sourceforge.net");
WebResponse response = webConversation.getResponse(request);
WebLink installLink = response.getLinkWith("Installation");
WebResponse installResponse = installLink.click();
}
}
We show here two possibilities, having the same result. We just need to add the new test case in the previous test suite:
package httpunit.test;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllHttpUnitTests
{
public static Test suite()
{
TestSuite suite;
suite = new TestSuite("AllHttpUnitTests");
suite.addTestSuite(httpunit.TestJumpJDev.class);
suite.addTestSuite(httpunit.TestJumpJDevTwo.class);
return suite;
}
public static void main(String args[])
{
String args2[] = {"-noloading", "httpunit.test.AllHttpUnitTests"};
junit.swingui.TestRunner.main(args2);
}
}
And we run it again!
Test Html Tables
Let's say our application generates an HTML Table, after the data stored in a database for example.
HttpUnit provides a way to test the data rendered by the table.
Let us imagine we want to test a document rendered as the following one:
Let us write another Test Case, again...
package httpunit;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.TableCell;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;
import junit.framework.TestCase;
/**
* We want to test HTML Tables
*/
public class TestThree extends TestCase
{
public TestThree(String name)
{
super(name);
}
public void testSailorsTable() throws Exception
{
WebConversation webConversation = new WebConversation();
WebRequest request = new GetMethodWebRequest("http://localhost:8988/JTidy-HttpUnit%40work-context-root/tableTest.html");
WebResponse response = webConversation.getResponse(request);
WebTable table = response.getTableStartingWith("Column One");
assertEquals("column count", 2, table.getColumnCount());
assertEquals("row count", 3, table.getRowCount()); // Warning: the header is a row!
TableCell cell = table.getTableCell(2, 1);
assertEquals("cell text", "Slocum", cell.asText());
}
public void testSailorsAndShipsTable() throws Exception
{
WebConversation webConversation = new WebConversation();
WebRequest request = new GetMethodWebRequest("http://localhost:8988/JTidy-HttpUnit%40work-context-root/tableTest.html");
WebResponse response = webConversation.getResponse(request);
WebTable table = response.getTableStartingWith("First Column");
assertEquals("column count", 3, table.getColumnCount());
assertEquals("row count", 4, table.getRowCount()); // Warning: the header is a row!
TableCell cell = table.getTableCell(2, 1);
assertEquals("cell text", "Slocum", cell.asText());
}
}
Just like before, add it to your Test Suite:
package httpunit.test;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllHttpUnitTests
{
public static Test suite()
{
TestSuite suite;
suite = new TestSuite("AllHttpUnitTests");
suite.addTestSuite(httpunit.TestJumpJDev.class);
suite.addTestSuite(httpunit.TestJumpJDevTwo.class);
suite.addTestSuite(httpunit.TestThree.class);
return suite;
}
public static void main(String args[])
{
String args2[] = {"-noloading", "httpunit.test.AllHttpUnitTests"};
junit.swingui.TestRunner.main(args2);
}
}
... and run:
Using HttpUnit in conjonction with JUnit will help you to test the behavior of your web applications! And once again,
those applications do not necessary need to be J2EE compliant. XP is not talking only about Java...