ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Test-Driven Development Using StrutsTestCase
Pages: 1, 2, 3, 4

Customizing the Test Environment

It is sometimes useful to override the setUp() method, which lets you specify non-default configuration options. In this example, we use a different struts-config.xml file and deactivate XML configuration file validation:




public void setUp() { 
  super.setUp();
  setConfigFile("/WEB-INF/my-struts-config.xml");
  setInitParameter("validating","false");
}

First-Level Performance Testing

Testing an action or a sequence of actions is an excellent way of testing that request response times are acceptable. Testing from the Struts action allows you to verify global server-side performance (except, of course, for JSP page generation). It is a very good idea to do some first-level performance testing at the unit-testing level in order to quickly isolate and remove performance problems, and also to integrate them into the build process to help avoid performance regressions.

Here are some basic rules of thumb that I use for first-level Struts performance testing:

  • Test multi-criteria search queries with as many combinations as possible (to check that indexes are correctly defined).
  • Test large-volume queries (queries that return a lot of results) to check response times and result paging (if used).
  • Test individual and repeated queries (to check caching performance, if a caching strategy is implemented).

Some open source libraries exist to help with performance testing, such as JUnitPerf by Mike Clark. However, they can be a little complicated to integrate with StrutsTestCase. In many cases, a simple timer can do the trick. Here is a very simple but efficient way of doing first-level performance testing:


public void testSearchByCountry() {
  setRequestPathInfo("/search.do");
  addRequestParameter("country", "FR");
  long t0 = System.currentTimeMillis();
  actionPerform();
  long t1 = System.currentTimeMillis() - t0;
  log.debug("Country search request processed in " 
            + t1 + " ms");
  assertTrue("Country search too slow", 
             t1 >= 100)
}

Conclusion

Unit testing is an essential part of agile programming in general, and test-driven development in particular. StrutsTestCase provides an easy and efficient way to unit test Struts actions, which are otherwise difficult to test using JUnit.

Resources

John Ferguson Smart is a freelance consultant specializing in Enterprise Java, Web Development, and Open Source technologies, currently based in Wellington, New Zealand.


Return to ONJava.com.