|
Unit Test Java Web Apps with Cactus
Extreme programming means testing often during the development life cycle, and the Cactus environment is ideal for making unit testing easier
by Kevin Jones
There has been a major movement in recent years towards Extreme Programming (XP), a trend that can be traced back at least ten years, if not more, and more recently espoused by Kent Beck and others in books such as Extreme Programming Explained (Addison-Wesley, 1999). One of the tenets of XP is "test early, test often." But why test?
Testing helps in many areas. It can show that a piece of code works; it can show that a piece of code still works after refactoring; and it helps reduce debugging time, which is often a large part of a developer's day, if not more. Often more time is spent in debugging than in writing the code! Developers love writing code, but they hate writing tests. However, for testing to be effective it has be comprehensive, which means writing lots of tests and checking the results of those tests. Both of these processes get tedious pretty quickly; however, while writing tests cannot yet be fully automated (despite the availability of tools, such as Parasoft's jTest, that provide pretty good coverage), it is possible to get the computer to check test results and display a succeeded or failed message.
Many Java developers add tests to their classes; others use a framework, the most popular being the open source JUnit testing framework available at www.junit.org. JUnit is a framework for writing unit tests.
So what is a unit test? A unit test is a highly localized test. A unit test class only tests within a single package; it doesn't test the interaction of one package with another, which is the job of functional testing.
Before writing a test case there has to be something to be tested. Recently, I found myself developing a blogging application. One of the pieces of data displayed on the Web page is a list of other sites that reference this page on any given day, with a referrer being a page that has followed a link to the site. A referrer needs two pieces of information, a hitcount and a URL. This information can be modeled in a JavaBean like this:
public class Referrer
implements Serializable
{
String URL;
int hitCount = 0;
public String getURL()
{
return URL;
}
public void setURL(
String URL)
{
this.URL = URL;
}
public int getHitCount()
{
return hitCount;
}
public void setHitCount(
int hitCount)
{
this.hitCount =
hitCount;
}
}
Back to top
|