Prev: Set Up
Next: Configuration
This page: TestCase | testMethodName | assertEquals | TestRunner |
junit.framework.TestCase is the superclass of all JUnit unit test classes. You will extend this class and add test methods for each unit test that you wish to complete. You will need to include the -classpath ".;c:\junit3.8.1\junit.jar" switch to compile and run TestRunner for your test cases. Professional developers include this in their build scripts.
// The Test Class itself
import junit.framework.*;
public class MyTestClass extends TestCase
{
}
To compile from a Windows command line prompt:
javac -classpath ".;c:\junit3.8.1\junit.jar" MyTestClass
Each unit test will be implemented as a method with the name testXxxxx
,
where Xxxxx
is as descriptive a name as you can provide.
Effective tests will test a desired outcome and behavior of a class.
This is usually done via the assertEquals( v1, v2 )
message.
import junit.framework.*;
public class MyTestClass extends TestCase
{
public void testMethodName() throws Exception
{
}
}
The TestCase class contains several overloaded versions of the assertEquals method. This method is used to confirm that each action produces the correct results.
import junit.framework.*; public class MyTestClass extends TestCase { public void testMethodName() throws Exception { assertEquals( 3, (int)Math.max(3,6) ); // a test that fails assertEquals( 3, (int)Math.min(3,6) ); // a test that passes } }
TestRunner is the Java program that will systematically run each of the testMethods that you wrote in your TestXxxxx class. It will also automatically track and report the successes and failures recorded. The console version is good, but the graphic versions allow you to see a GREEN BAR when all tests pass. If any test fails, you will see a RED BAR instead. This visual information regarding the successes and failures of your implementation is not only useful, it is great fun to see.
Each of these commands assumes that you are in the parent directory
of the junit/samples/AllTests
file. Otherwise the JUnit directory "c:\junit3.8.1"
must be in your path system variable.
java -classpath ".;c:\junit3.8.1\junit.jar" junit.textui.TestRunner junit.samples.AllTests
java -classpath ".;c:\junit3.8.1\junit.jar" junit.awtui.TestRunner junit.samples.AllTests
java -classpath ".;c:\junit3.8.1\junit.jar" junit.swingui.TestRunner junit.samples.AllTests
Last modified: 7/25/2003
Feedback, questions or accessibility issues.
Copyright © 2003 Deb Deppeler
All rights reserved.