9

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.

4
  • You are outputting the result of <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.
    – dcsohl
    Apr 29, 2014 at 15:22
  • I’m sure this is related to issues.jenkins-ci.org/browse/JENKINS-22716; I’m experiencing the same issue in my Jenkins installation.
    – Bombe
    Apr 29, 2014 at 20:42
  • No jenkins involved atm. I tried it with routing to a wrong path to the jacoco.exec and the build fails then. So the file is found. I post the whole script in a minute. Thanks guys!
    – mkuff
    Apr 30, 2014 at 7:05
  • It works now. I corrected the error and have now coverage. Thanks a lot for your input!
    – mkuff
    Apr 30, 2014 at 8:19

2 Answers 2

3

I had the wrong directory referenced in the report structure. The newer version of jacoco printed a warning which helped to spot the problem. Corrected my script in the original post. Maybe it helps someone sometime.

1

It's wierd. In my case, I used Java JDK 1.8.0_45 to "gradle clean build" and "gradle jacocoTestReport". Got the same error as Oscar mentioned above with Gradle.

jacocoXX.exec file was at the correct location.

Ran "gradle clean build" when JDK was 1.8.0_45, then ran "gradle jacocoTestReport" with JDK 1.7.0_40 -- It worked like a charm.

One other solution is: If you want to use JDK 1.8.0_45 (i.e. Java8) for both build and jacocoTestReport tasks, then call like: gradle -x compileJava -x test -x classes -x testClasses jacocoTestReport (then the error doesn't come).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.