HttpUnit and Netbeans


HttpUnit is a tool to support automated unit testing of your Java web app.

The objective of this brief tutorial is to add a short unit test to the wrestler database application.

Download HttpUnit and unzip it in any convenient directory of your choice.

Launch NetBeans.

First we want to create a new library to contain the HttpUnit components.
From the "Tools" menu select "Libraries". The "Library Manager" appears.
Click the "New Library" button.  The "New Library" dialog appears.
For the library name, enter "HttpUnit".
Leave the library type as "Class Libraries".  Click OK.
Click "Add JAR/folder".
Navigate to the "lib" folder of the HttpUnit distribution and select the file "httpunit.jar".
In a similar manner there are three other jar files to add to the library.  They are found in the "jars" folder of the HttpUnit distribution:
js-1.6R5.jar
nekohtml-0.9.5.jar
xercesImpl-2.6.1.jar
The Library ClassPath should now have all four jar files listed.  Click OK to close the Library Manager.

From the Project panel, open the IFPWAFCAD project from a previous tutorial.
Run the project and wait for Tomcat to start and for the index page to appear in your browser.

Expand the project tree and right-click on "Test Libraries".  From the context menu, select "Add Library". 
In the dialog that appears you should see the newly created "HttpUnit" library listed. Select it and click the "Add Library" button.  Expand the "Test Libraries" tree and all four jar files should appear.

Now you can create a unit test.
Right-click on "Test Packages" and select "New" then "Java Class".
In the dialog that appears provide a class name of "TestIndex" and click "Finish".
A skeleton Java class should appear in the edit window.

Enter the Java code for the unit test provided below.

Right-click on "TestIndex.java" in the project panel and select "Run File".
The JUnit Test Results should appear in the output window indicated "PASSED."

Troubleshooting

If the test case doesn't run, check the port that your Tomcat instance is using. The test case uses port 8084 and some student configurations use 8080. You can modify the URL used in the test case in this line:
WebRequest request = new GetMethodWebRequest("http://localhost:8084/IFPWAFCAD/index.jsp");


import com.meterware.httpunit.*;
import junit.framework.*;

/** Test class for the sample IFPWAFCD web app */
public class TestIndex extends TestCase
{
    /**
     * Verifies that submitting the form with the subject id  "2" results
     * in a page containing the title "Existential Psychotherapy"
     **/
    public void testSubmitForm() throws Exception
    {
        WebConversation     conversation = new WebConversation();
        WebRequest  request = new GetMethodWebRequest(
                  "http://localhost:8084/IFPWAFCAD/index.jsp" );

        WebResponse response = conversation.getResponse( request );
        WebForm submitForm = response.getForms()[0];
        request = submitForm.getRequest();
        request.setParameter( "subject_id", "2" );
        response = conversation.getResponse( request );
        assertEquals( "Page title", "Existential Psychotherapy", response.getTitle() );
    }

}


Resources:

Testing Web Applications with HttpUnit (Don't worry, it's not boring!)

HttpUnit Cookbook