Program testing can be conducted at different levels of program granularity: expressions, statements, individual methods, public methods, and the user interface. Testing at both fine and coarse levels of granularity is essential for achieving program reliability. Testing at a fine-grain of granularity is called unit testing. The goal of unit testing is to check the behavior of each identifiable unit of program behavior in isolation from all other units. The smallest unit of program behavior that is accessible in compiled Java code is the individual method (excluding private methods which cannot be accessed directly).
The practice of unit testing can be a very effective tool for building robust programs of any size. A strong suite of tests, developed in tandem with the code they test, can aid our efforts to develop robust code in many respects. Errors can be caught more quickly. Code can be refactored more easily. Other programmers can understand the functionality of other programmer's code more quickly. But writing unit tests to maximum advantage requires that we employ several important principles.