Monday, June 19, 2006

Classpath not working in Ant junit task

Pop quiz: spot the error in the following Ant fragment:

<path id="common.classpath">
<!-- some classpath content -->

<path id="common.test.classpath">
<path refid="common.classpath"/>
<pathelement location="${basedir}/lib/junit.jar"/>
<pathelement location="${basedir}/build/common/test/bin"/>

<target name="test" depends="compile-tests"
<junit fork="yes" forkmode="once" printsummary="on">
<path refid="${common.test.classpath}"/>

<formatter type="plain" />

<fileset dir="common/test/java" includes="**/*.java"/>

You may not even need to know ant syntax to spot the error, since it's an internal inconsistency.

The clue is that you get "class not found" errors for all tests when you run the "test" target.

Give up?

The nested classpath element for "test" is not referring to an existing path ID. I confused an Ant property with an Ant ID. It should say

<path refid="common.test.classpath"/>

without the ${} wrappers that are used to refer to Ant properties. There's a correct path reference in the test classpath definition itself.

No comments: