Home

Introduction

Getting Started

Junit TestRunner

Links








Junit TestRunner


Now that you have a basic understanding on how to use Junit's testing tools, the next thing you will want to do is run the program. To illustrate how this is done we will continue with our example, SimpleTest.java, which was discussed in "Getting Started". Copy the example to your favorite text editor and save it as SimpleTest.java. If you copied the program word for word than the next line, which is the command necessary to compile your program into platform-independent byte code should not give you any errors.

javac -classpath .:/usr/share/java/junit.jar SimpleTest.java

There are three possible interfaces that Junit offers when running tests, these interfaces are known as the Junit TestRunners. Make sure SimpleTest.java is in its original state before using each of the testRunners:

TextUI: Provides text-based output to stdout.

AwtUI: Provides GUI-based output using the AWT (Abstract Window Toolkit) from Java.

SwingUI: Provides GUI-based output using the Swing graphical user interface component kit from Java.

To select an interface you will execute the following command sequence:

java -classpath .:/usr/share/java/junit.jar junit.USERINTERFACE.TestRunner classfile

The following will explain how to use and interpret each of the Junit textRunners.


TextUI

Issue the following command once you have compiled SimpleTest.java:

java -classpath .:/usr/share/java/junit.jar junit.textui.TestRunner SimpleTest

You should see output similar to this:

.F 1
Time: 0.01
2
There was 1 failure:
1) testSimpleTest(SimpleTest)junit.framework.AssertionFailedError: expected:<2> but was:<3>
3
             at SimpleTest.testSimpleTest(SimpleTest.java:9)

             at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
             at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
             at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

FAILURES!!!
Tests run: 1, Failure: 1, Errors: 0
4

1  .F = False

2  The time it took the TextRunner to complete the testing

3  Information on where the error occurred and what the error was

4  The final statistics of the overall testing

Now that you know what an error looks like, lets correct the problem and run the program again. Set the value of answer = 2, recompile the program and run TextRunner. You should see output similar to this:

.
Time: 0

OK (1 tests)

AwtUI

Issue the following command once you have compiled SimpleTest.java:

java -classpath .:/usr/share/java/junit.jar junit.awtui.TestRunner SimpleTest

You should see a window similar to this:

1  You can run as many test classes as you want by simply supplying the class name and clicking "Run". Remember to supply the correct classpath.

2  The final statistics of the overall testing

3  Lists the errors that occurred in the program. If you click on the error you will see information on it in the screen below.

4  Information on where the error occurred and what the error was

  The time it took the GUIRunner to complete the testing

Now that you know what an error looks like, lets correct the problem and run the program again. Set the value of answer = 2, recompile the program and run GUIRunner. You should see output similar to this:


SwingUI

Issue the following command once you have compiled SimpleTest.java:

java -classpath .:/usr/share/java/junit.jar junit.swingui.TestRunner SimpleTest

You should see a window similar to this:

1  You can run as many test classes as you want by simply supplying the class name and clicking "Run". If you are unsure of the correct classpath, click on the "..." button and find what you are looking for.

2  The final statistics of the overall testing

3  Lists the errors that occurred in the program. If you click on the error you will see information on it in the screen below. There is another tab called "Test Hierarchy" that displays the same information as the "Failures" tab but in a different way.

4  Information on where the error occurred and what the error was

  The time it took the SwingRunner to complete the testing

Now that you know what an error looks like, lets correct the problem and run the program again. Set the value of answer = 2, recompile the program and run SwingRunner. You should see output similar to this:


Back to Top