JUnit's de facto status in the Java community has resulted in coverage tools tailoring their documentation to facilitate coverage via JUnit test cases. TestNG, however, is an exciting framework, which addresses some of the short comings in JUnit with flexible fixtures, parameterized tests, and dependent tests (to name a few).
Using a coverage tool like Cobertura requires a few key modifications to an Ant build file. First, follow the first two steps outlined in the Cobertura documentation (initializing the task and instrumenting the code under test). Next, utilize the TestNG Ant task; however, add the following aspects: (i) include the instrumented code in the TestNG task's [b]classpath[/b] attribute, (ii) create a [b]sysproperty[/b] which points to the binary report file for Cobertura, and (iii) add a [b]classpath[/b] element which includes Cobertura's classpath, which was most likely defined when initializing Cobertura.
For example, a combined coverage-TestNG Ant target would look like this:
code
<target name="testng-cov" depends="testng-init,instrument">
<mkdir dir="${testng.output.dir.comp}"/>
<testng groups="unit,component,system"
outputDir="${testng.output.dir.comp}"
sourceDir="${testng.source.dir}"
classpath="${cov.inst.dir};${testclassesdir};${classesdir}">
<sysproperty key="net.sourceforge.cobertura.datafile"
file="${basedir}/cobertura.ser" />
<classfileset dir="${testng.source.dir}">
<include name="**/*Test.java"/>
</classfileset>
<classpath>
<path refid="cobertura.classpath" />
<path refid="build.classpath"/>
</classpath>
</testng>
<mkdir dir="${cov.report.dir}"/>
<cobertura-report format="html"
datafile="${basedir}/cobertura.ser"
destdir="${cov.report.dir}" srcdir="${src.dir}" />
</target>
[/code]
For more information on TestNG see http://www.testng.org or see Filippo Diotalevi's "TestNG makes Java unit testing a breeze" (http://www-128.ibm.com/developerworks/java/library/j-testng/).