Getting Started with JUnit


810:188
Agile Software Development


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.


Introduction

JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.


Downloading and Installing JUnit

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.

  1. Unzip the junit.zip file. Place it in the directory of your choice.

    Confirm that you have a complete installation by comparing the contents of your junit3.8.1 directory to the expected contents.

  2. Add junit.jar to your CLASSPATH.

    How to do this is system-specific. Here are some instructions that we have learned from experience on some particular platforms.

    If you use a different system or have any problems setting your classpath using these instructions, please let me know.

  3. Test your installation by using one of JUnit's interfaces to run the suite of tests that comes with JUnit. All of the tests should pass.

    First, change into the junit3.8.1 directory. Then...

You are now ready to begin programming test-first!


Getting Started with JUnit

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:


Documentation

Here is some of the documentation that comes with JUnit:


Full Contents of the Release

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:


Possible Problems

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.


Eugene Wallingford ==== wallingf@cs.uni.edu ==== February 6, 2003