"Jeremy W. Redmond" wrote:
>
> Supposedly HttpUnit is a good way to test servlets.  I have only read about
> it, but have yet to try it.
>
> http://httpunit.sourceforge.net/
>

Here are a couple of examples that might give the flavor of HttpUnit and
Cactus.

HttpUnit is a tool for making HTTP requests and processing the
responses. When using it, you write tests that basically simulate a user
requesting a page with a browser. The following probably doesn't
compile, but it should give you the idea:

  public void testHello()
  {
        WebConversation conv = new WebConversation();
        WebRequest req = new GetMethodWebRequest("http://host/hello";);
        WebResponse resp = conv.getResponse(req);

        // The response object includes a DOM represenation of
        // the returned page and utility methods for accessing
        // parts of it.

        assertEquals("hello world", resp.getTitle());
  }

The WebConversation will manage cookies so you can simulate a session.

HttpUnit has a servlet testing component under development, but I
haven't looked at it.

Another servlet testing tool is Cactus from Jakarta.

  http://jakarta.apache.org/cactus/

Cactus tests run inside your web application. You don't use Cactus to
test overall servlet behavior (you wouldn't test doGet with Cactus, for
example). Instead, Cactus is for writing unit tests for objects and
methods which expect to run in a servlet container. For example, in an
MVC application, you might want to unit-test some action classes.

If you have the following action class:

  public class MyAction implements WebAction
  {
     public void doAction(HttpServletRequest req,
                          HttpServletResponse resp)
     { ... }
  }

then a Cactus test might look like:

  public void testMyAction()
  {
        MyAction action = new MyAction();

        // Cactus provides the request and response objects.
        action.doAction(request, response);

        // Check that the action set the response header.
        assertTrue(response.containsHeader("Content-Type"));
  }

Since Cactus tests run as part of your web application, you have access
to things your web application sets up at startup (connection pools,
etc) in your tests.

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to