JUnit Quick Guide
From Craig
The purpose of this document is to provide a quick guide to using JUnit. Much more complete documentation is available from the JUnit website (http://www.junit.org).
| Table of contents |
Obtain JUnit.jar
You need to obtain a copy of the JUnit code. This is stored in a jar file (java archive file) called junit.jar. You can download this from the course website: junit.jar (http://pages.cpsc.ucalgary.ca/~schock/wiki/data/code/cpsc219/L17/junit.jar)
This file contains all of the compiled classes necessary for using JUnit.
Create your test classes
In order to create a test class you need to do the following:
- Create a new test class file.
- import junit.framework.*; into your test class
- Your test class must be a subclass of the TestCase
- All methods which contain test code must start with the word test. (Note, all lowercase)
- Optionally use the setUp() and tearDown() methods to set up structures necessary for your tests.
- Compile your test class (Please see below)
- Run your tests (Please see below)
Compiling your test classes
Your test classes are reliant on code which is contained within the junit.jar file. This file must be considered by the compiler when it compiles your test class. A simple way to do this is to add the -classpath directive to your compilation:
javac -classpath .:junit.jar java files to compile
The -classpath directive specified above tells the compiler to look into the current directory AND the junit.jar file when compiling the specified class files. If you get a compiler error "Class definition not found", that generally means that your classpath is not set properly. NOTE: The above example assumes that the junit.jar file and all of the code to be compiled are all contained within the same directory.
Running your tests
JUnit comes with three separate test runner programs:
- A text-based test runner
- A test runner based on the AWT windowing system (ugly)
- A test runner based on the Swing windowing system (only slightly less ugly)
The text based test runner is useful if you are remotely logged into a server and do not have direct access to a graphical display. The Swing-based test runner is nicer than the runner based on the AWT. To run each, do the following
Text-based test runner
java -classpath .:junit.jar junit.textui.TestRunner Name of test class
This will run a text based version of the test runner. Any errors will be reported to the terminal window within which the TestRunner is executed. The test runner terminates after the tests have been executed. In order to rerun the tests, the program must be re-executed.
GUI based test runner
To run a GUI based version of the test runner, execute the following:
java -classpath .:junit.jar junit.swingui.TestRunner Name of test class
The GUI based version has 3 main advantages:
- The program does not terminate after the test have been run. The tests can be executed again by pressing the "Run" button.
- The output is much easier to read and understand.
- Obtaining information about specific tests is much easier using the GUI based version. Using the GUI, individual tests can be rerun.
Running Multiple Tests
When a large system is tested it will likely contain many test methods spread over multiple test classes. It is possible to easily consolidate all of the tests to be executed within a single test class. In order to do this, one needs to create a suite. This can be accomplished by creating a class which consolidates all of the test classes into a single test suite. An example is seen below:
In file AllTests.java:
import junit.framework.*;
public class AllTests
{
public static Test suite()
{
TestSuite suite = new TestSuite("Name of Test Suite");
suite.addTestSuite(PersonTest.class);
suite.addTestSuite(CommunityTest.class);
return suite;
}
}
In this case, the tests which are consolidated are those tests contained within the classes PersonTest and CommunityTest. If more test classes are added, they have to be included within the suite here. To run the consolidated version of the tests, simply execute the following:
java -classpath .:junit.jar junit.swingui.TestRunner AllTests
This will cause all of the tests defined within PersonTest.java and CommunityTest.java to be executed.
Use JUnit=
As mentioned in class, the typical development process is:
- Edit
- Compile
- Run
A much better process is:
- Edit
- Compile
- Test
- Run
If your new code doesn't pass unit testing, there is no point to running the program; the program failed its tests. By utilizing this process, you are much less likely to introduce errors into your program which break code which is already working. Using this testing framework has the potential to considerably reduce your debugging time.
