Java Reference
In-Depth Information
registration procedure. A registration method would at least inventory which tests
are running.
Another approach would be to use Java's reflection and introspection capabilities.
A program could look at itself and decide to run whatever methods follow a certain
naming convention—like those that begin with test , for example.
Making it easy to add tests (the third rule in our earlier list) sounds like
another good rule for a unit testing framework. The support code to realize this
rule (via registration or introspection) wouldn't be trivial, but it would be worth-
while. There'd be a lot of work up front, but that effort would pay off each time
we added a new test.
Happily, the JU nit team has saved us the trouble. The JU nit framework already sup-
ports introspecting methods. It also supports using a different class instance and class
loader instance for each test and reports all errors on a test-by-test basis.
Now that you have a better idea of why you need a unit testing framework, let's
look specifically at JU nit.
1.4
JUnit design goals
The JU nit team has defined three discrete goals for the framework:
The framework must help us write useful tests.
The framework must help us create tests that retain their value over time.
The framework must help us lower the cost of writing tests by reusing code.
We return to these goals in chapter 2.
Next, before we get into the action, we'll show you how to set up JU nit.
1.5
Setting up JUnit
In order to use JU nit to write your application tests, you need to add the JU nit JAR
file to your project's compilation classpath and to your execution classpath. Follow
these steps:
Download the JU nit distribution (junit-4.6 or newer) from http://www.junit.org.
JU nit contains several test samples that you'll run to get familiar with executing
JU nit tests.
Unzip the distribution zip file to a directory on your computer system (for exam-
ple, C:\ on Windows or /opt/ on UNIX ).
In this directory, unzipping will create a subdirectory for the JU nit distribution you
downloaded (for example, C:\junit4.6 on Windows or /opt/junit4.6 on UNIX ).
You're now ready to run the tests provided with the JU nit distribution. JU nit comes
complete with Java programs that you can use to view the result of a test, including a
text-based test runner with console output (figure 1.2).
To run the text test runner, open a shell in C:\junit4.6 on Windows or in /opt/
junit4.6 UNIX , and type the appropriate command for your operating system:
 
 
 
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search