Java Reference
In-Depth Information
S
TEPPING
THROUGH
A
TEST
For each test (
test
XXX
methods in the
YYY
TestCase
classes), the six steps shown in
figure 14.2 take place. Let's step through them.
Execute
beginXXX
.
If there is a
begin
XXX
method, Cactus executes it. The
begin
XXX
method lets you pass information to the redirector. The
TestSam-
pleServletIntegration
example extends
ServletTestCase
and connects to
the Cactus servlet redirector. The servlet redirector is implemented as a servlet;
this is the entry point in the container. The Cactus client side calls the servlet
redirector by opening an
HTTP
connection to it. The
begin
XXX
method sets up
HTTP
-related parameters that are set in the
HTTP
request received by the serv-
let redirector. This method can be used to define
HTTP
POST
/
GET
parameters,
HTTP
cookies,
HTTP
headers, and so forth.
Here's an example:
1
public void beginXXX(WebRequest request) {
request.addParameter("param1", "value1");
request.addCookie("cookie1", "value1");
[...]
}
In the
TestSampleServletIntegration
class, we've used the
begin
XXX
method
to tell the redirector not to create an
HTTP
session (the default behavior cre-
ates one):
public void beginIsAuthenticatedNoSession(WebRequest request) {
request.setAutomaticSession(false);
}
Open the redirector connection.
The
YYY
TestCase
opens a connection to its redi-
rector. In this case, the
ServletTestCase
code opens an
HTTP
connection to
the servlet redirector (which is a servlet).
2
Create the server-side
TestCase
instance.
The redirector creates an instance of
the
YYY
TestCase
class. Note that this is the second instance created by Cactus;
the first one was created on the client side (by the
JU
nit
TestRunner
). Then the
redirector retrieves container objects and assigns them in the
YYY
TestCase
instance by setting class variables.
In the servlet example, the servlet redirector creates an instance of
Test-
SampleServletIntegration
and sets the following objects as class variables in it:
HttpServletRequest
,
HttpServletResponse
,
HttpSession
, and others. The
servlet redirector is able to do this because it's a servlet. When it's called by the
Cactus client side, it has received a valid
HttpServletRequest
,
HttpServlet-
Response
,
HttpSession
, and other objects from the container and is passing
them to the
YYY
TestCase
instance. It acts as a proxy/redirector (hence its name).
The redirector then starts the test (see step 4). Upon returning from the test,
it stores the test result in the
ServletConfig
servlet object along with any
exception that might have been raised during the test, so the test result can
3