Installing JUnit

Problem

You need to configure JUnit so you can run your tests using Ant. Although you have added junit.jar to your classpath, you still see errors.

Solution

You have three possible solutions:

  1. Install Ant’s optional.jar as well as JUnit’s junit.jar in the ANT_HOME /lib directory.

  2. Ensure that neither optional.jar nor junit.jar is in the ANT_HOME /lib directory. Then set up a classpath in your buildfile that includes both JAR files.

  3. Ensure that neither optional.jar nor junit.jar is in the ANT_HOME /lib directory. Then set your CLASSPATH environment variable to include both JAR files.

Discussion

Ant’s junit task is implemented by a class named JUnitTask, which is found in the optional.jar file that ships with the Ant distribution. Ant includes many so-called “optional” tasks, which generally depend on external libraries in order to function. In the case of the junit task, junit.jar is required. It is your responsibility to download JUnit and properly configure it to work with Ant.

Class loading problems are common in cases where optional Ant tasks depend on external libraries such as junit.jar. The Java ClassLoader instance that loads the JUnitTask class must also be able to load various JUnit classes. For the proper classes to be visible, you must follow one of the three solutions that were just mentioned.

You generally install Ant’s optional.jar in the ANT_HOME /lib directory, so the easiest way to configure JUnit is to also install junit.jar in ANT_HOME /lib. Example 3-6 ...

Get Java Extreme Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.