woensdag, februari 20, 2008

java.lang.NoClassDefFoundError: net/sourceforge/cobertura/coveragedata/HasBeenInstrumented

Ik was aan de slag met een ant build file waarbij junit en cobertura gebruikt worden:

<target description="Run JUnit tests" depends="compile" name="test">
<mkdir dir="${dir.report}">
<mkdir dir="${dir.junit}">
<junit showoutput="yes" printsummary="yes" haltonfailure="no">
<sysproperty file="${dir.cobertura}/cobertura.ser" key="net.sourceforge.cobertura.datafile">
<classpath>
<pathelement location="${dir.instrumented}">
<pathelement location="${dir.build}">
<pathelement location="${tmp.dir.classes}">
<pathelement path="${java.class.path}">
</classpath>
<formatter type="xml">
<batchtest todir="${dir.report}/junit">
<fileset dir="${tmp.dir.classes}">
<include name="**/*Test.class">
</fileset>
</batchtest>
</junit>
<junitreport todir="${dir.report}/junit">
<fileset dir="${dir.report}/junit">
<include name="TEST-*.xml">
</fileset>
<report todir="${dir.junit}" format="frames">
</report>
</junitreport>
</target>

Effect: verschillende NoClassDefFoundErrors in de junit-reports als ik de ant task run vanuit Eclipse, maar ook vanaf de command-line. De unittest draaien via de JUnittest wizard gaat prima.

Oplossing: Ant gebruikt de ant-junit-task om unittests uit voeren en voor 1.6.* wordt het boot-classpath genegeerd. Alle classes die de task dus nodig hebt moet je in geval van ant command line in de ANT_HOME/lib folder zetten, voor Eclipse moet je dezelfe classes toevoegen via Preferences->Ant->Runtime.