advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : StrutsTestCase: The Tool for Struts Unit testing :

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.


How to Add Java Applets to Your Site

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs