C SC 205: Implementation of Software Components
Autumn 2007
Pete Sanderson
Unit Testing with JUnit and jGRASP
Note: this document is based on JUnit 3.8.1.
Obtaining JUnit
You will need the file junit.jar. I have placed a copy on the "I:" drive at
I:\CSC\205\20074-CSC-205-01\Common. Copy it into the folder where you do your Java development.
I will hereafter call this folder csc205.
Alternatively, you can download it from www.junit.org. Just be
sure you download version 3.8.1, because that is the version this document is based on. Major changes
were made starting with version 4.0
Configuring jGRASP to work with JUnit
Follow this sequence of steps to configure jGRASP to work nicely with JUnit:
- From the Settings menu, select PATH/CLASSPATH, then Workspace.
- Select the CLASSPATHS tab from the resulting dialog box, it not
already selected. It should look like this:

- Click New and browse to the junit.jar you previously copied to your csc205
folder. Ignore the Documentation Path field. It will look something like this:

- Click OK, to get back to the original dialog box. The file path you just entered
will be listed and the dialog box will look like this:

- Click Apply then OK.
- The dialog boxes are gone. From the Settings menu, select Compiler Settings, then Workspace.
- Select the Compiler tab from the resulting dialog box, if not already selected,
then the Flags/Args/Main subtab. It should look like this:

- Click the black square in row Run and column Flags2, then enter
junit.swingui.TestRunner into the text field. The result should look like this:

- Click Apply then OK.
- IMPORTANT! With this setting, the JUnit tester will run every time you
you want to run a program, even if you are no longer using JUnit! To "suspend" the setting, follow
steps 6-9 again. This time, when you click the box, the word junit.swingui.TestRunner
disappears. It will be recovered the next time you click the box; it is only in suspension.
Steps 1-5 are necessary for your test driver (below) to compile. The compiler needs to know where to
find the JUnit JAR file. Steps 6-9 are necessary to use JUnit's graphical user interface at runtime -- this is
not mandatory and JUnit will run fine with text output if this is not done.
Writing the Test Driver using JUnit
The purpose of unit testing is to test the various methods of a class under development.
Your test driver must be a separate public Java class that looks something like this. The
bolded text will change for your specific class but the rest is standard.
import junit.framework.*;
public class MyTestDriver extends TestCase {
///////////////////////////////////////////////
// Define any instance variables, constructors
// or helper methods needed by the driver here.
public MyTestDriver() {
}
///////////////////////////////////////////////
// Place your test methods here. A test method
// must meet these requirements:
// 1. public void
// 2. no parameters
// 3. name starts with "test"
// 4. calls the method(s) being tested
// 5. contains a pass/fail test, usually in form of an assertion
public void testFirstMethod() {
}
public void testSecondMethod() {
}
// Define additional tests as needed...
/////////////////////////////////////////////////
// These are standard, except the test class name
// has to go into the TestSuite constructor call.
public static junit.framework.Test suite() {
return new TestSuite(MyTestDriver.class);
}
public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}
}
|
Here's an example of a test method to test the size() method in ArrayList.
Note: To get the best feedback when a test fails, arguments to assertEquals()
and other assert methods should be listed with the expected value first, then the actual value.
public void testEmptyArrayList() {
ArrayList a = new ArrayList();
assertEquals("Size of empty list is not 0", 0, a.size() );
}
|
Hee's an example of a test method in the situation where the method will throw an exception
if it is working properly:
public void testForException() {
ArrayList a = new ArrayList();
try {
Object obj = a.get(0);
fail("expected IndexOutOfBoundsException");
}
catch (IndexOutOfBoundsException success) {}
}
|
The fail() and assertEquals() methods are inherited through
TestCase in the junit.framework package, which in turn inherits
them from the Assert
class. Click the link to see its API. The assertEquals() method in particular
is overloaded to the hilt.
Running the Test
This is the easy part. The test driver you created above contains main() so can be run from
jGRASP. When you run it, all the test methods will be applied and their results displayed in a dialog box. If the horizontal bar ends
up GREEN, all the tests passed. If it is RED, one or more tests failed.
Here's an example of a successful test:

If you get a RED bar, check to see which test(s) failed. Then the debugging starts!
If the test results show up in the JGRASP text output window instead, this just means that the
compile flags configuration (steps 6-9 under Configuring jGRASP) did not work. Here's the same
example with text output:
[ C
SC 205 | Peter
Sanderson | Math Sciences server
| Math Sciences home page
| Otterbein ]
Last updated:
Peter Sanderson (PSanderson@otterbein.edu)