Written by Olivier Le Diouris, Oracle Corporation
February 2004.
Unit testing gained popularity with the adoption of eXtreme
Programming (XP). JUnit introduced the possibility to run unit tests in Java.
Several other components running on top of JUnit can be used to run unit tests
concerning some specific technologies, like XML and HTTP. This document intends
to explain how to use HttpUnit on top of JUnit, from Oracle JDeveloper 10g.
Get the required software
HttpUnit can be found at sourceforge.
In this example we are going to use the version 1.5.4.
Once you've downloaded the HttpUnit archive, unzip it on your system. In the generated directory structure, you will
find several directories, including a lib, and a jar one.
You'll also need JUnit installed on your system, and the JUnit
Extension installed in Oracle JDeveloper. Installing the extension will create
a library called JUnit Runtime.
We will refer to the location where you've installed HttpUnit
as HTTPUNIT_HOME.
Create the required library
In order to refer to the HttpUnit engine, you will need to create a library inside JDeveloper:
In JDeveloper, go to Tools | Manage Libraries.
Choose to create a new User Library
Call it HttpUnit Runtime, we will refer to like with this
name later on
You are now ready to write your first HttpUnit test case.
Let us say we want for that first one to check if we can reach the page located
at http://jumpjdev.sourceforge.net.
We must proceed as follow:
Create in the workspace of your choice a new project
Associate with it the libraries called JUnit Runtime and HttpUnit Runtime
Create a new java class as follow:
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;
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);
}
}
Now, using File | New | General | Unit Tests (JUnit), create
a new TestSuite, and refer to the TestCase you've created above to populate
the TestSuite. It should look like this:
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);
}
}
Depending on the fact that the page you want to reach is inside
or outside the firewall, you might have to set some proxy. It can be done
from JDeveloper.
Once this is done appropriatly, you can run the newly generated TestSuite.