Interview Questions

How Do You Run JUnit Using Ant?

JUnit Questions and Answers


(Continued from previous question...)

How Do You Run JUnit Using Ant?

The following answer is suggested by Eric Armstrong:

1. Define any necessary Ant properties:

<property name="src" value="./src" />
<property name="lib" value="./lib" />
<property name="classes" value="./classes" />
<property name="test.class.name" value="com.xyz.MyTestSuite"/>

2. Set up the CLASSPATH to be used by JUnit:

<path id="test.classpath">
  <pathelement location="${classes}" />
  <pathelement location="/path/to/junit.jar" />
  <fileset dir="${lib}">
    <include name="**/*.jar"/>
  </fileset>
</path>

3. Define the Ant task for running JUnit:

<target name="test">
  <junit fork="yes" haltonfailure="yes">
    <test name="${test.class.name}" />
    <formatter type="plain" usefile="false" />
    <classpath refid="test.classpath" />
  </junit>
</target>

4. Run the test:

ant test

(Continued on next question...)

Other Interview Questions