494 WebSphere Version 5 Application Development Handbook
The first part of this chapter is a generalized discussion about unit testing. We
define what we mean by the term unit testing, and discuss the benefits and
problems associated with including a unit testing strategy in a development
process. Finally we describe some of the benefits you may obtain from basing
your unit tests upon an existing unit testing framework.
What is unit testing?
Before we delve into the details, let us first explain what unit testing means to us.
During the course of a development project you often encounter many different
terms associated with different types of testing, and it is common practice to
dedicate individuals, teams, or even entire departments to the testing function.
Unit tests, however, are informal tests that are generally executed by the
developers of the application code. They are often quite low-level in nature, and
test the behavior of individual software components such as individual Java
classes, servlets, or EJBs.
Because unit tests are usually written and performed by the application
developer, they are often white-box in nature, that is to say, they are written
using knowledge about the implementation in mind, to test specific code paths,
for example. This does not mean that all unit tests have to be written this
way—one common practice is to write the unit tests for a component based on
the component specification before developing the component itself. Both
approaches are valid—when defining your own unit testing policy you may want
to make use of both.
Why unit testing?
On the face of it, this is a question with a straightforward answer. We test to find
defects in our code, and to verify that the changes we have made to existing
code do not break that code. Perhaps it is more useful to look at the question
from the opposite perspective, that is to say, why don’t developers perform unit
In general, the simple answer is because it is too hard, and because nobody
forces them to do so. Writing an effective set of unit tests for a component is not
a trivial undertaking. Given the pressure to deliver that many developers find
themselves subjected to, the temptation is often overwhelming to postpone the
creation and execution of unit tests in favor of delivering code fixes or new