Testing error situations
The failures we've encountered should remind us that every
test program must also test the error situations. Luckily STC
can handle this situation as well. I mentioned that the
detail action needs an index value to function,
and if we omit it we'll get an error as seen above:
. . .
F
Time: 1,6
There was 1 failure:
1) testActions(hansen.playground.TestStrutsAction)
junit.framework.AssertionFailedError:
was expecting '/detail.jsp' but received '/error.jsp'
|
Since the
DetailDVDAction class forwards to error in two
situations, we'll have to check which value was given to the
Struts ActionError method. The test for a missing
index sends the value "error.noindex":
. . .
// Test the detail action - without an index
setRequestPathInfo("/detail");
actionPerform();
verifyActionErrors(new String[] {"error.noindex"});
|
When we run the test program once more we receive no error
messages.
If you continue your program with a new test
in the same test-method then you should realize that the
request object now contains a non-null
ActionErrors object. If you don't clear it the next test
using verifyNoActionErrors() will fail. To clear it,
insert this statement in your code:
getRequest().setAttribute(Globals.ERROR_KEY,null);
The Globals.ERROR_KEY may be imported from
org.apache.struts.Globals. See the complete test
program here .
Conclusion
An important part of unit testing a Struts application is to
test if the execute method of the Action classes
work properly. STC sets up a simulated Struts environment that
makes it very simple to code these tests. Since STC uses JUnit
your test programs will fit nicely into the collections of unit
tests you probably already have made for your application.
I've shown you how to code and run STC programs, and you've
also seen that there may be some pieces missing in the simulated
environment. A way to get an idea of what will work and what
might not work is to browse through the STC JavaDoc. If you, for
example, spend some time looking at the JavaDoc for the
MockStrutsTestCase, you'll also see that there are
features which we haven't covered yet.
In the next article we'll see how easy it is to replace the
mock object environment with the Cactus framework, thereby using
the real servlet environment set up by the web server. We'll
also see how to handle the "setup once for all method
calls" situation in an elegant way, and there'll be some
other goodies to.
Continue on to Part 2
Resources
Keld is currently working
as a web architect for one of the largest IT companies in Denmark. He
battled with the mainframes during the 70's when they were the size
of a gymnasium and had the power of your PalmPilot. He also struggled
with CASE-tools in the 90s and now explores the cutting edge
technology of the Web. While not busy at his computer he likes to
vacation on the Greek islands.
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.