Declares a junit task in Ant like this

build.xml

  <!-- Run jUnit -->
  <target name="junit" depends="resolve">

	<junit printsummary="yes" haltonfailure="no">

		<classpath refid="test.path" />
		<classpath location="${build.dir}" />
			
		<test name="com.mkyong.test.TestMessage" 
			haltonfailure="no" todir="${report.dir}" outfile="result">
			<formatter type="plain" />
			<formatter type="xml" />
		</test>

	</junit>
  </target>

Run ant junit, but hits the following error message :


BUILD FAILED
build.xml:86: The <classpath> for <junit> must include junit.jar if not in Ant's own classpath

Solution

To run a junit task in Ant, make sure the junit.jar is defined in the classpath.

build.xml

  <!-- Run jUnit -->
  <target name="junit" depends="resolve">

	<junit printsummary="yes" haltonfailure="no">

		<classpath refid="test.path" />
		<classpath location="${build.dir}" />
						
		<!-- Make sure these two libraries are included -->
		<classpath location="lib/junit-4.11.jar" />
		<classpath location="lib/hamcrest-core-1.3.jar" />
			
		<test name="com.mkyong.test.TestMessage" 
			haltonfailure="no" todir="${report.dir}" outfile="result">
			<formatter type="plain" />
			<formatter type="xml" />
		</test>

	</junit>
  </target>

References

  1. Ant jUnit task
  2. Ant FAQ : junit ignores my classpath