I am trying to integrate jacoco to our ant build and evaluate it with a simple test project.
The compilation and the other output looks promising, but when I look at the coverage it is always zero.
package alg;
public class SpecialAlgorithm {
public SpecialAlgorithm() {}
public int uncoveredMethod(int i) {
return i * i;
}
public int sum(int i, int j) {
return i + j;
}
}
Testcase:
package alg;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import alg.SpecialAlgorithm;
public class SpecialAlgorithmTest {
@Test
public void testSum() {
SpecialAlgorithm alg = new SpecialAlgorithm();
int sum = alg.sum(1, 2);
assertEquals(3, sum);
}
}
Ant script:
<project xmlns:jacoco="antlib:org.jacoco.ant" name="Code Coverage with JaCoCo"
default="rebuild">
<property name="src.dir" location="../java" />
<property name="test.dir" location="../../test/java" />
<property name="result.dir" location="c:/temp/jacoco/target" />
<property name="result.classes.dir" location="${result.dir}/classes" />
<property name="result.report.dir" location="${result.dir}/site/jacoco" />
<property name="result.exec.file" location="${result.dir}/jacoco.exec" />
<!-- Step 1: Import JaCoCo Ant tasks -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="../../../lib/jacocoant.jar" />
</taskdef>
<target name="clean">
<delete dir="${result.dir}" />
<mkdir dir="${result.dir}" />
<mkdir dir="${result.dir}/site/jacoco/" />
</target>
<target name="compile" depends="clean">
<mkdir dir="${result.classes.dir}" />
<javac srcdir="${src.dir}" destdir="${result.classes.dir}"
debug="true" includeantruntime="false">
<classpath>
<pathelement location="${result.classes.dir}" />
</classpath>
</javac>
</target>
<target name="test" depends="compile">
<taskdef name="junit"
classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
<classpath>
<pathelement location="../../../lib/junit.jar" />
<pathelement location="../../../bin" />
<pathelement location="${result.classes.dir}" />
</classpath>
</taskdef>
<jacoco:coverage destfile="${result.dir}/jacoco.exec"
xmlns:jacoco="antlib:org.jacoco.ant">
<junit fork="yes" forkmode="once" dir="." failureProperty="test.failed">
<classpath>
<pathelement location="../../../lib/junit.jar" />
<pathelement location="../../../bin" />
<pathelement location="${result.classes.dir}" />
</classpath>
<formatter type="xml" />
<batchtest todir="${result.report.dir}">
<fileset dir="${test.dir}"/>
</batchtest>
</junit>
</jacoco:coverage>
<!-- Step 3: Create coverage report -->
<jacoco:report>
<!-- This task needs the collected execution data and ... -->
<executiondata>
<file file="${result.exec.file}" />
</executiondata>
<!-- the class files and optional source files ... -->
<structure name="JaCoCo Ant Example">
<classfiles>
<fileset dir="../../../bin" />
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}" />
</sourcefiles>
</structure>
<!-- to produce reports in different formats. -->
<html destdir="${result.report.dir}" />
<csv destfile="${result.report.dir}/report.csv" />
<xml destfile="${result.report.dir}/report.xml" />
</jacoco:report>
</target>
<target name="rebuild" depends="test" />
</project>
UPDATE: After upgrading to the newer version I get a warning which may indicate what my mistake is.
Console output:
Buildfile: JacocoEvaluation\src\main\resources\coverage.xml
clean:
[delete] Deleting directory c:\temp\jacoco\target
[mkdir] Created dir: c:\temp\jacoco\target
[mkdir] Created dir: c:\temp\jacoco\target\site\jacoco
compile:
[mkdir] Created dir: c:\temp\jacoco\target\classes
[javac] Compiling 1 source file to c:\temp\jacoco\target\classes
test:
[jacoco:coverage] Enhancing junit with coverage
[jacoco:report] Loading execution data file c:\temp\jacoco\target\jacoco.exec
[jacoco:report] Writing bundle 'JaCoCo Ant Example' with 1 classes
[jacoco:report] Classes in bundle 'JaCoCo Ant Example' do no match with execution data. For report generation the same class files must be used as at runtime.
[jacoco:report] Execution data for class alg/SpecialAlgorithm does not match.
rebuild:
BUILD SUCCESSFUL
Total time: 1 second
The directory structure (like maven):
main/java/ Class to Test test/java/ Testcase
Any ideas what I am doing wrong? The plugin works fine.
UPDATE: Works now. I had the wrong directory referenced in the report structure. The newer version of jacoco printed a warning which helped to spot the problem.
<jacoco:coverage>
to${result.dir}/jacoco.exec
, and then<jacoco:report>
is looking in${result.exec.file}
when building its report. Are these two variables pointing to the same place? (You didn't include enough of your ant script for me to tell.) You should consider using one variable across the board to reduce the risk of this sort of mismatch.