I have adapted this help sheet from the README file that comes with JUnit 3.8.1, both to simplify it and to fill in some details that you may find helpful.
JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.
You can download JUnit from my site (v3.8.1) or from www.junit.org. The result is a zip file named junit.zip.
To install JUnit, do the following.
Confirm that you have a complete installation by comparing the contents of your junit3.8.1 directory to the expected contents.
How to do this is system-specific. Here are some instructions that we have learned from experience on some particular platforms.
This is a common Unix shell, and the default shell for Mac OS X users.
This is also a common Unix shell, and the default shell for Linux users.
The instructions for different versions of Windows seem to vary, but I have collected what we know about 98, NT, and XP (the OS, not the agile methodology!) into one help file..
If you use a different system or have any problems setting your classpath using these instructions, please let me know.
First, change into the junit3.8.1 directory. Then...
java junit.textui.TestRunner junit.samples.AllTests
java junit.awtui.TestRunner junit.samples.AllTests
java junit.swingui.TestRunner junit.samples.AllTests
You are now ready to begin programming test-first!
To get started with unit testing and JUnit, read Test Infected - Programmers Love Writing Tests. This article appeared in the Java Report several years ago and introduced the world to JUnit. You can also find this article in <junit3.8.1/doc/testinfected/testing.htm>.
Test Infected demonstrates the development process with JUnit using a set of classes for arithmetic on money and currencies. The source code for this paper is in junit/samples/money/.
You can find some other examples in the junit.samples package:
Here is some of the documentation that comes with JUnit:
A complete junit3.8.1/ directory contains:
README.html | a superset of this file, with fewer details |
cpl-v10.html | JUnit's license |
junit.jar | a jar file with the JUnit framework and tools |
src.jar | a jar file with the source code of the junit framework |
junit | the source code of the JUnit samples |
javadoc | javadoc-generated documentation |
doc | documentation and useful articles |
The junit directory contains two subdirectories:
Don't place junit.jar into the extension directory of your JDK installation. If you do, your system will not be able to find the test classes.
The reason that you have to switch into the junit3.8.1 directory to run the tests is that JUnit's test suite is not in the junit.jar file. If you add INSTALL_DIR/junit3.8.1/ to your classpath, you should be able to run the tests from any directory, as well as access the sample classes that come with JUnit from anywhere, too.