< Zurück | Inhalt | Weiter >

4.2.2 Unit Testing Made Easy

Why all the fuss about main() and command-line parameters? Such main() methods are a handy way to provide unit tests for a class. The tests can be controlled by the command-line parameters. By testing each class you can re- duce the time to integrate the parts of an application. Furthermore, a set of unit tests can be built up (e.g., as shell scripts) to provide automated regression tests for the entire project. As a more rigorous and systematic approach to unit testing, we discuss junit in Chapter 13.


4.3 THE System CLASS

The Java System class provides some of the answers to questions about our environment. What follows is not an exhaustive discussion of all the methods in the System class, but only of those areas that touch on our specific focus— input/output (I/O) and environment variables.

Be aware that all of the methods in the System class are static. There- fore you never need to (and you can’t) call a constructor on System. You just use the “class name, dot, method name” syntax to call the method (e.g., System.getProperties()). Similarly, the accessible fields in System are all static, so for some of the I/O-related methods you use the “class name, dot, field name, dot, method name” syntax (e.g., System.out.println()). As of Java 5.0, you can shorten this, by using a static import, that is:


import static java.lang.System.*;


Then in your other references you can leave off System, for example,

getProperties() and out.println().