Developer Tools
JDeveloper
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.
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);
}
}
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);
}
}
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();
}
}
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);
}
}
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());
}
}
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);
}
}
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...