What Is Unit Testing?
A unit is defined as the smallest piece of functionality that can stand alone. In C++ and Java applications, a unit generally is seen as equivalent to a class. If the class has been well designed and deals with a distinct object, the one class equals one unit test is a good rule of thumb. Unit tests are one of the many concepts in Ant in which consistency in conventions is important and can reduce the effort required to automate the process.
As an example, let's consider a simple class with a method that adds an object to the end of a list. Without unit testing, you probably would have performed some basic testing to make sure that the object was added to the end of the list, not to the beginning or somewhere in the middle. Unit testing provides the confidence that the method will do the proper thing, no matter what the input is. For example, consider what the sample method would do if the inputs were as follows:
The object is null.
The list is null.
The list is empty.
The list has exactly one item.
These are the types of tests that often are not conducted. At some point, a section of code that calls the sample class might be modified so that the list can be empty. The application no longer operates correctly, but the bug is more difficult to locate because what appeared to be working code that has not been changed is now broken. Rightly so, the focus is placed on the new code that was written rather than the code where the bug actually exists. By ensuring that the code can properly deal with all types of inputs, this method can be used in a variety of circumstances. It will be unlikely that the code is hiding a dormant bug, which always seems to appear at the worst possible time.