January 17, 2021
Hot Topics:

Automated Unit Testing Frameworks

  • By Yasser EL-Manzalawy
  • Send Email »
  • More Articles »

JUnit in Action

In this section, I will show you a step by step example to write JUnit tests. To run this example, you need to obtain JUnit from its official site at www.junit.org. Installing JUnit is very simple; just unzip the downloaded file and place junit.jar in your CLASSPATH.

The class to be tested is a Java Queue class as presented in the article "Queue: A missed java.util Class." The public interface to the Queue class is shown in Listing 1.

public class Queue {
   private LinkedList items;
   public Queue()
   public Object enqueue (Object element)
   public Object dequeue ( )
   public int size ( )
   public boolean empty ( )
   public void clear ( )

Listing 1: Fragment of the Queue class

Step 1: Imports

You need to import the junit.framework package to write your test case.

import junit.framework.*;

Step 2: Implement a Test Case

A test case is a class derived from junit.framework.TestCase.

public class TestQueue extends TestCase  {

   public TestQueue (String name) {
      super (name);

   public static void main ( String[] args)  {
      junit.textui.TestRunner.run (suite());

Just use the constructor to invoke the TestCase constructor. The main() method is used to run the test runner textual interface.

Step 4: Define and Initialize the Fixture

In this example, we need to create two Queue objects, an empty queue and a queue of three Integer objects. Each test in the test case may send some messages to one of the two objects. So, we have to re-initialize the two objects before running each test. In other test cases, you also may need to tear down some objects after running each test (for example, close a network or database connection). JUnit provides two methods, setUp() and tearDown(), that you can override in your test case to initialize and tear down any member variables in the test case.

To implement the TestQueue fixture, add two private Queue references and override the setUp () method.

private Queue empty_queue;
private Queue queue;

   protected void setUp() {
   empty_queue = new Queue();
   queue       = new Queue ();
   queue.enqueue (new Integer (1));
   queue.enqueue (new Integer (2));
   queue.enqueue (new Integer (3));

Step 5: Compose a Test Suite

Add the following static method to your test case.

public static Test suite() {
   return new TestSuite(TestQueue.class);

This way, you will make use of the JUnit Reflection-driven API and you will be restricted to start the name of any test with the word "test." In addition, you can't assume anything about the order in which your tests will be run.

If you want to force a specific order of tests, not use the test's naming convention, run some other tests from other test cases, or eliminate some tests in the test case. You may write some code like this:

public static Test suite ( ) {
   TestSuite suite= new TestSuite("Customized Test Suite");
   suite.addTest(new TestSuite(TestQueue.class));
   return suite;

Page 2 of 3

This article was originally published on June 23, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date