The next test program
The way you organize JUnit programs is that you create many
test-methods, each testing a specific feature. We'll therefore
add a new method to test the "detail" action, which
brings us from the List page to the Detail page:
public void testDetail() {
setRequestPathInfo("/detail");
addRequestParameter("index","1");
actionPerform();
verifyForward("detail");
verifyNoActionErrors();
}
|
The detail action needs to carry a parameter holding
the index for the DVD to be detailed. In the web application
this is done with a hyperlink:
http://localhost:8080/dvdlib/detail.do?index=1
So
you use the addRequestParameter method to add parameters
to the request. The index value could as well come from
an HTML form field. The Action class would not be able to
tell the difference.
If we run this program we're once again surprised:
. . .
F
Time: 1,97
There was 1 failure:
1) testDetail(hansen.playground.TestStrutsAction)
junit.framework.AssertionFailed
Error: was expecting '/detail.jsp' but received '/error.jsp'
at servletunit.struts.Common.verifyForwardPath(Common.java:313)
. . .
FAILURES!!!
Tests run: 2, Failures: 1, Errors: 0
|
Looking in the code for the
DetailDVDAction class we can see there are two cases
where we'll forward to error.jsp: either the index
value is not given (and we didn't forget that) or the DVD
collection stored in session scope by the list action is
null. The answer to the failure above is that STC works like
JUnit: each test method starts from scratch by calling the
setUp method, and you can not pass data from one test
method to another.
What we could do was to let the
setUp method create the DVD collection and save it in
session scope, but I'll use the simpler approach where we use
one single test method. If the list action is called
first, everything should be OK. Let's try that:
public void testActions() {
setContextDirectory(new File(
"d:\\apache\\jakarta-tomcat-4.1.12\\webapps\\dvdlib"));
setRequestPathInfo("/list");
actionPerform();
verifyForward("list");
verifyNoActionErrors();
setRequestPathInfo("/detail");
addRequestParameter("index","1");
actionPerform();
verifyForward("detail");
verifyNoActionErrors();
}
|
Now the test shows no errors:
. . .
INFO: ActionServlet: Reading dvds.xml: 3 dvds read
Time: 2,74
OK (1 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.