Java Reference
In-Depth Information
T
ESTING
THE
CALL
V
IEW
METHOD
We have enough in place now that we can write the tests for
callView
, as shown in list-
ing 14.6.
To make the test easier to read, we create a
createCommandResult
private method.
This utility method creates arbitrary DynaBean objects, like those that will be returned
by
executeCommand
. In
testCallView
, we place the DynaBeans in the
HTTP
request
where the
JSP
can find them.
Listing 14.6
Unit tests for
callView
[...]
public
class
TestAdminServlet
extends
ServletTestCase {
[...]
private
Collection createCommandResult()
throws
Exception {
List results =
new
ArrayList();
DynaProperty[] props =
new
DynaProperty[] {
new
DynaProperty("id", String.class),
new
DynaProperty("responsetime", Long.
class
)
};
BasicDynaClass dynaClass =
new
BasicDynaClass("requesttime",
null
, props);
DynaBean request1 = dynaClass.newInstance();
request1.set("id", "12345");
request1.set("responsetime",
new
Long(500));
results.add(request1);
DynaBean request2 = dynaClass.newInstance();
request1.set("id", "56789");
request1.set("responsetime",
new
Long(430));
results.add(request2);
return
results;
}
public
void
testCallView()
throws
Exception {
AdminServlet servlet =
new
AdminServlet();
// Set the result of the execution of the command in the
// HTTP request so that the JSP page can get the data to
// display
request.setAttribute("result", createCommandResult());
servlet.callView(request);
}
}
There's nothing we can verify in
testCallView
, so we don't perform any asserts there.
The call to
callView
forwards to a
JSP
. But Cactus supports asserting the result of the
execution of a
JSP
page. We can use Cactus to verify that the
JSP
will be able to display
the data that we created in
createCommandResult
. Because this would be
JSP
testing,
we show how it works in section 14.4 (“Testing
JSP
s”).
Listing 14.7 shows the
callView
method that we use to forward the execution to
the
JSP
in order to display the results.