Step 3: Add four jar-files to the setcp bat file from my first
article: cactus, commons-httpclient, commons-logging, and aspectjrt.
The setcp file is placed in the classes directory, and it defines the
classpath for the client test program.
Listing 1: setcp.bat - a Windows bat-file that sets the classpath for the
client
@echo off
set TOMCATROOT=d:\apache\jakarta-tomcat-4.1.12\
set APPDIR=%TOMCATROOT%\webapps\cactusdvdlib
set LIBDIR=%APPDIR%\web-inf\lib\
set CP=.
set CP=%CP%;%TOMCATROOT%common\lib\servlet.jar
set CP=%CP%;%APPDIR%
set CP=%CP%;%LIBDIR%struts.jar
set CP=%CP%;%LIBDIR%strutstest-2.0.0.jar
set CP=%CP%;%LIBDIR%junit.jar
set CP=%CP%;%LIBDIR%cactus.jar
set CP=%CP%;%LIBDIR%aspectjrt.jar
set CP=%CP%;%LIBDIR%commons-httpclient.jar
set CP=%CP%;%LIBDIR%commons-logging.jar
rem Remove the 'rem' if jdk1.4 is NOT used
rem set CP=%CP%;%TOMCATROOT%common\lib\xerces.jar
rem JDOM is used by the DVDManager class
set CP=%CP%;%TOMCATROOT%common\lib\jdom.jar
set CLASSPATH=%CP%
set CP=
|
Note that TOMCATROOT must point to the directory where your Tomcat server is
installed.
Step 4: Add the Cactus configuration file cactus.properties to
cactusdvdlib/WEB-inf/classes
(then it will be in the classpath defined in step 3):
Listing 2:
# Configuration file for Cactus.
cactus.contextURL = http://localhost:8080/cactusdvdlib
cactus.servletRedirectorName = ServletRedirector
|
Note that we assume the server is running on port 8080 (as Tomcat does as
default). The URL is used by the STC test program when it sends its request to
the web server, and it must contain the name of the directory for the project.
Step 5: Add two Cactus servlet definitions to your web-xml
file. The ServletRedirector is the program that receives the servlet
request from your test program and executes the server side code. The ServletTestRunner
may be used to run your test program from a browser.
Listing 3:
. . .
<servlet>
<servlet-name>ServletRedirector</servlet-name>
<servlet-class>
org.apache.cactus.server.ServletTestRedirector
</servlet-class>
</servlet>
<servlet>
<servlet-name>ServletTestRunner</servlet-name>
<servlet-class>
org.apache.cactus.server.runner.ServletTestRunner
</servlet-class>
</servlet>
. . .
<servlet-mapping>
<servlet-name>ServletRedirector</servlet-name>
<url-pattern>/ServletRedirector</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletTestRunner</servlet-name>
<url-pattern>/ServletTestRunner</url-pattern>
</servlet-mapping>
. . .
|
To see if the ServletRedirector servlet is working, enter
http://localhost:8080/cactusdvdlib/ServletRedirector?
Cactus_Service=RUN_TEST
If you get a blank page back, the servlet works!
Step 6: Use the class CactusStrutsTestCase instead of
MockStrutsTestCase in your test program. Let's use the test program from the
first article:
Listing 4:
package hansen.playground;
import servletunit.struts.*;
import java.io.*;
import org.apache.struts.*;
public class TestStrutsAction extends CactusStrutsTestCase {
public void setUp() throws Exception { super.setUp(); }
public void tearDown() throws Exception { super.tearDown(); }
public TestStrutsAction(String testName) { super(testName); }
public void testActions() {
// Test the list action
setRequestPathInfo("/list");
actionPerform();
verifyForward("list");
verifyNoActionErrors();
. . .
}
public static void main(String[] args) {
junit.textui.TestRunner.run(TestStrutsAction.class);
}
}
|
Compile the program (using the setcp.bat file from Listing 1), start
or restart your web
server and run the test program.
If everything works it will produce an output like this:
D:...cactusdvdlib\WEB-INF\classes>
java hansen/playground/TestStrutsAction
.
Time: 2,56
OK (1 test)
|
Errors, either when compiling or running the test program, are probably because
of incorrect classpath setup. Check the spelling of files in the setcp.bat file,
and check that you didn't forget any of the 6 steps above. If the error is still
there, deploy your test case to both the client and the server targets,
and restart your server. If nothing helps
then check the Cactus FAQ
and the STC FAQ.
Did you notice the time taken to run the test program above? 2,56 seconds is
considerably more than when we used the mock object set up. Cactus has a much
more complex set up--you get what you pay for--a more realistic test.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.