5.4 Checking a Static Web Page

5.4.1 Problem

You want to test for the existence of a static web page.

5.4.2 Solution

Write a JUnit test fixture, and then use classes in the com.meterware.httpunit package to connect to the web page.

5.4.3 Discussion

When starting a brand new web application, verify that you can connect to the home page before doing anything else. This is the simplest test that you can write, and only takes a few lines of code. Example 5-1 shows how to do it.

Example 5-1. Testing for a static web page
package com.oreilly.javaxp.httpunit;

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

public class TestNewsletter extends TestCase {
    public TestNewsletter(String name) {
        super(name);
    }

    public void testIndexPageExists(  ) throws Exception {
        WebConversation webConversation = new WebConversation(  );
        WebRequest request =
                new GetMethodWebRequest("http://localhost:8080/news");

        WebResponse response =
                webConversation.getResponse(request);
    }
}

The WebConversation class is described as HttpUnit's replacement for the web browser. WebConversation is the client, allowing you to send requests and obtain responses from a web server.

Use the GetMethodWebRequest class to issue an HTTP GET request, specifying a URL in the constructor. This mimics a user typing in a URL in their web browser. Then call the getResponse( ) method to actually issue the request and retrieve the web page. If getResponse( ) cannot find the web page, it throws a new instance of HttpNotFoundException. As customary in JUnit, you should not catch this exception. Instead, let the JUnit framework catch the exception and treat it as a test error.

Since requesting a page is such a common operation, HttpUnit offers a simplified syntax that achieves the same results:

WebConversation webConversation = new WebConversation(  );
WebResponse response =
        webConversation.getResponse("http://localhost:8080/news");

We do not care about the content of the response. Instead, we are merely checking for the existence of the home page. Because of this, we can simplify the example even further. First, we get rid of the response object because we never refer to it:

WebConversation webConversation = new WebConversation(  );
webConversation.getResponse("http://localhost:8080/news");

And finally, we can inline the code into a single statement. In this case, we feel that inlining makes the code easier to read, particularly because the name of our test method explains what we are trying to test. Clarity is almost always more important than brevity, so you should avoid inlining if it makes the code harder to understand. Here is the final unit test:

public void testIndexPageExists(  ) throws Exception {
    // this throws HttpNotFoundException if the home page is not found
    new WebConversation(  ).getResponse("http://localhost:8080/news");
}

If you set up your Ant buildfile and Tomcat as explained in Recipe 5.3, you can type ant junit to run this test.

5.4.4 See Also

Recipe 5.3 shows how to set up the build environment. Recipe 4.13 shows how to handle exceptions using JUnit.