JUnit: Basic Use

Prev: Set Up
Next: Configuration

This page: TestCase | testMethodName | assertEquals | TestRunner |


Quick Start

  1. Write a class that extends the junit.framework.TestCase class.
  2. Compile your class by using the -classpath switch.
  3. Run your test case class by using the -classpath switch. This will cause a "No Test Cases found" error.
  4. Add a testMethod to your TestCase class.
  5. Fix your program code until the tests all pass.
  6. Write new tests and repeat until all program requirements are met and all tests pass.

TestCase

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
{

}

Use the -class path switch to compile a JUnit TestCase class

To compile from a Windows command line prompt:

javac -classpath ".;c:\junit3.8.1\junit.jar" MyTestClass

testMethodName

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
    {

    }
}

assertEquals

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

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.

Run TestRunner to see the results of your testMethods

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.

Console Window
java -classpath ".;c:\junit3.8.1\junit.jar" junit.textui.TestRunner junit.samples.AllTests


AWT GUI Frame
java -classpath ".;c:\junit3.8.1\junit.jar" junit.awtui.TestRunner junit.samples.AllTests


Swing GUI Frame
java -classpath ".;c:\junit3.8.1\junit.jar" junit.swingui.TestRunner junit.samples.AllTests