Java Development With Ant: Cactus

Cactus is an in-container unit testing framework layered on top of JUnit. This document will just briefly describe how it is used in this project.

Testing Struts Actions

This project contains an example test case using StrutsTestCase, which is a layer on top of Cactus or can operate in "mock" object mode.  The example test case uses the Cactus mode.  SearchFormTest is:
package org.example.antbook.struts;

import servletunit.struts.CactusStrutsTestCase;
import junit.framework.TestSuite;
import junit.framework.Test;

public class SearchFormTest extends CactusStrutsTestCase {
public SearchFormTest(String s) {
super(s);
}

public void testValidation() {
addRequestParameter("query","");
setRequestPathInfo("/search");
actionPerform();
verifyActionErrors(new String[] {"query.required"});
verifyInputForward();
}

public static Test suite() {
return new TestSuite(SearchFormTest.class);
}
}

All methods called in testValidation() are StrutsTestCase API calls, with the last two methods performing JUnit assertions to ensure that the expected validation error is returned and that Struts forwards to the input page appropriately.  The request being made has an empty query parameter, which is validation error (see SearchForm, and note @struts.validator type="required").

To run the Cactus test, first deploy the application and start the application server.  Then run the "test-web" Ant target.  There are more automated ways to run Cactus tests, but not really feasible to do unless the target application server is known and has been coded to work with Cactus and therefore it is done more manually in this project.  Consult the excellent Cactus documentation for details on how to automate testing further.