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>

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

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

<formatter type="plain" />

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

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

<classpath>
<path refid="common.test.classpath"/>
</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: