Getting Started with HttpUnit and JDeveloper 10g

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.

Get the required softwares

In this example



ms.net

Create the HttpUnit library

HttpUnit Runtime
  • 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)

JUnit at work

Follow hyperlinks


      
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);
  }
}
     
                            

JUnit at work

Test Html Tables


HTML Tables

      
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);
  }
}
     
                            

HTML Tables

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...


false ,,,,,,,,,,,,,,,