Introduction


You can write the coolest Struts applications in the world, but if you don't know how to test them, or you can't seem to debug them, you might as well not have written them in the first place. Testing and debugging web applications can be a frustrating experience. If you test your application by deploying it to your application server and "banging on it," then the recipes in this chapter will provide some techniques that are worth learning. If your only approach to debugging involves sticking System.out.println( ) in places where you think the problem might be, then you'll definitely want to check out some of these solutions.

The key to debugging an application is visibility; the more information you have about the internal workings of the application, the easier it can be to pinpoint the problem. The chapter has solutions that show you how to monitor what your application is doing. This first half of this chapter presents some solutions related to logging and debugging that will help you understand what's going on inside your application.

The heart of testing and debugging is the unit test. A unit test verifies that a specific portion of your application does what it is supposed to do. Java developers commonly think of the "unit" as a Java class. But the unit can be anything as long as it can be identified by discrete boundaries and isolated from code external to the unit. This chapter has two recipes that show you how to unit test your Struts actions.

In "traditional" software testing, the tester ensures that the application functions as the user expects. You can apply the concepts of unit testing to functional testing by considering a scenario as the "unit." Use cases such as "Login," "New User Registration," or "Place an Order" to drive your unit tests. These functional unit tests are as valid as the low-level tests, though these tests can be more difficult to isolate from outside effects. If you follow the best practices of web application designusing the Model-View-Controller (MVC) design pattern supported by Strutsyour most important unit tests will be testing your application's model and business services. Struts-specific tests should comprise a small portion of your application's entire unit test suite.

Without a doubt, the JUnit (http://www.junit.org) framework, developed by Erich Gamma and Kent Beck, provides an elegant, extensible framework that has become the de facto standard for Java unit testing. JUnit provides the base test class, TestCase, that you extend to create your specific unit test. You write test methods that exercise the unit under test and verify the unit behaves as expected for different inputs.

JUnit provides the test harness for running your tests. JUnit treats any method in your test class that begins with the word "test" as a method to run; your test methods will have names like testSearchByName(), testSuccess( ), etc. JUnit runs your test by instantiating your test class and calling these test methods. Within each test method, you exercise the class under test and verify the results using assertion methods defined in the TestCase base class. An assertion expresses a Boolean (true/false) relationship. For example, you may assert that a value returned from a method is not null, or that the returned value has a specific value. If an assertion fails, that particular test method will fail. You can run your tests using command-line and graphical test runners included with JUnit. Many popular Java development environments, such as Eclipse and IDEA, come with built-in JUnit test runners. You can also run your tests as part of your application's Ant build script.

The second half of this chapter shows you some recipes that make it easier to write unit tests for your Struts application. The solutions make use of a number of excellent open source extensions to JUnit such as StrutsTestCase, Cactus, and JWebUnit.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net