January 16, 2021
Hot Topics:

Test Cases Made Easy with JUnit 4.5

  • By David Thurmond
  • Send Email »
  • More Articles »

Since its introduction several years ago, JUnit has become an indispensible tool for Java developers for writing automated test cases. Now, JUnit 4.5 has taken unit testing to a whole new level with a new, more flexible architecture and some long-overdue new features. Here, you will learn how JUnit 4.5 makes writing test cases even easier than ever before.

Getting Started

To use JUnit 4.5, you will need to download it at http://sourceforge.net/project/showfiles.php?group_id=15278&package_id=12472 or go to http://www.junit.org and click on the Download JUnit link. The download is just a single .jar file, junit-4.5.jar. This file includes the latest framework for writing JUnit test cases and test suites, as well as the original JUnit 3.8 classes for backwards compatibility. JUnit 4.5 relies on assertions, a Java language feature introduced in JDK 1.5, so you will need to use JDK 1.5 or later to compile and run the examples. To begin writing test cases and test suites, all you need to do is include the .jar file in your classpath.

Although JUnit test cases can be executed at the command line by using the java command with the appropriate runner class, it is handy to use the Ant <junit> tag for this purpose instead. You will need to download Ant version 1.7.1 or later from http://ant.apache.org/bindownload.cgi to do so. Once you have installed Ant, you will need to copy junit-4.5.jar to the ANT_HOME/lib directory, so that Ant can make the necessary calls to JUnit to process the <junit> tag. The examples in this article are executed using Ant, and a build.xml file demonstrating the use of the <junit> tag is included in the source code.

Obviously, before you can write any test cases, you will need something to test. Listing 1 shows a simple program, NumberCruncher.java, with a handful of simple number calculation functions. The test cases in the examples refer to this code, which contains the following functions:

  • Public Boolean isPrime(Integer theNumber): Returns true if the integer passed in is a prime number.
  • Public Integer factorial(Integer theNumber): Returns the factorial value of the number passed in. For example, 6 factorial = 1 x 2 x 3 x 4 x 5 x 6 = 720.
  • Public Object[] getFactors(Integer theNumber): Returns an array of Integer objects that are factors of the number passed in.
  • Public Integer doForever(): This method would return an integer value, but actually performs an infinite loop.

Listing 1: NumberCruncher.java

package com.dlt.developer.numbers;

import java.util.ArrayList;

public class NumberCruncher {

   public boolean isPrime(Integer theNumber) {
      boolean value = true;
      for (int i = 2; i < theNumber.intValue(); i++) {
         if (theNumber.intValue() % i == 0) {
            value = false;
         }    // if
      }       // for i
      return value;
   }          // isPrime()

   public Integer factorial(Integer theNumber)
      throws IllegalArgumentException {
      if (theNumber.intValue() < 1) {
         throw new IllegalArgumentException("Factorial cannot be
            computed for numbers less than 1.");
      }       // if
      int value = 1;
      for (int i = 2; i <= theNumber.intValue(); i++) {
         value = value * i;
      }       // for
      return new Integer(value);
   }          // factorial()

   public Object[] getFactors(Integer theNumber) {
      ArrayList factors = new ArrayList();
      for (int i = 1; i <= theNumber.intValue(); i++) {
         if (theNumber.intValue() % i == 0) {
            factors.add(new Integer(i));
         }    // if
      }       // for i
      return factors.toArray();
   }          // getFactors()

   public Integer doForever() {
      Integer theValue = new Integer(0);
      boolean finished = false;
      while (!finished) {
         theValue = new Integer(theValue.intValue()+1);
      }       // while
      return theValue;
   }          // takeForever()

}             // NumberCruncher

The next section demonstrates how to write a test case using JUnit 4.5 to test out some of the functionality in NumberCruncher.java, and highlights some changes between versions 4.5 and 3.8.

Page 1 of 5

This article was originally published on November 12, 2008

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