Unit Testing: Avoid the Need for Debuggers
Problem
You don’t want to have to debug your code.
Solution
Use unit testing to validate each class as you develop it.
Discussion
Stopping to use a debugger is time-consuming. Better to test beforehand. The methodology of unit testing has been around for a long time, but has been overshadowed by newer methodologies. Unit testing is a tried and true means of getting your code tested in small pieces. Typically, in an OO language like Java, unit testing is applied to individual classes, in contrast to “black box” testing where the entire application is tested.
I have long been an advocate of this very basic testing methodology. Indeed, developers of the software methodology known as Extreme Programming (XP for short; see http://www.extremeprogramming.org) advocate writing the unit tests before you write the code, and also advocate running your tests almost every time you compile. This group of extremists has some very well-known leaders, including Gamma and Beck of Design Patterns fame. While I am not yet ready to unconditionally endorse all aspects of Extreme Programming, I certainly go along with their advocacy of unit testing.
Indeed, many of my classes come with a “built-in” unit
test. Classes that are not main programs in their own right often
include a main
method that just
tests out the functionality of the class. Here is an
example:
/** A simple class used to demonstrate unit testing. */ public class Person { protected String fullName; ...