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
Time: 0.01
There was 1 failure:
1)
testSimpleTest(SimpleTest)junit.framework.AssertionFailedError:
expected:<2> but was:<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
|
.F = False
The time it took the TextRunner to complete the testing
Information on where the error occurred and what the error was
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:
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:
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.
The final statistics of the overall testing
Lists the errors that occurred in the program. If you click on the error
you will see information on it in the screen below.
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:
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.
The final statistics of the overall testing
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.
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 |