Junit Coverage using Jacoco with ant build

I have run into this specific problem while executing the junit with jacoco:coverage for code coverage. Tried a few things but no luck yet.

I have this junit build script.

<target name="executeJunitMain" depends="createJunitLibs" description="Executes All Junit and prepare report.">

            <junit fork="yes" haltonfailure="off" failureProperty="junit.failure" includeantruntime="true" maxmemory="256m">
                <classpath refid="compile.class.path" />
                <formatter type="xml" />
                <jvmarg value="-javaagent:${external.junit.lib.dir}/jmockit.jar"/>
                <sysproperty key="jacoco-agent.destfile" file="${coverage.dir}/jacoco.exec"/>
                <batchtest fork="yes" todir="${report.dir}" >
                    <fileset dir="${dest.dir}">
                        <include name="**/Test*.class" />
                        <exclude name="**/AllTests.class" />
                        <exclude name="**/*$*.class"/>
                    </fileset>
                </batchtest>
            </junit>

        <antcall target="report"/>

    </target>

<!-- Execute the coverage report.-->
    <target name="report" description="Collect Coverage Report">

        <!-- Creates the Junit Report -->
        <junitreport todir="${report.dir}">
            <fileset dir="${report.dir}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" todir="${report.dir}/html"/>
        </junitreport>

        <!-- Creates the coverage Report.-->
        <mkdir dir="${report.dir}/coverage"/>
        <jacoco:report>
            <executiondata>
                <file file="${coverage.dir}/jacoco.exec" />
            </executiondata>

            <structure name="Code Coverage">
                <classfiles>
                    <zipfileset src="${sources.lib.dir}/${test.jar.name}.jar"/>
                                     </classfiles>
            </structure>
            <html destdir="${report.dir}/coverage" />
        </jacoco:report>

    </target>

using this ant script sometimes the junit build is failing. Error as below

BUILD FAILED
C:\Users\user\Codebases\junit.xml:111: The following error occurred while executing this line:
C:\Users\user\Codebases\junit.xml:147: The following error occurred while executing this line:
C:\Users\user\Codebases\junit.xml:164: Unable to read execution data file C:\Users\user\CodeBases\ju
nit\coverage\jacoco.exec

But sometimes it works perfectly. I am not sure when it works and when it does not. Any help will be appreciated.

Thanks

Upvotes: 2

Views: 7954

Answers (1)

I have found the issue that was causing the jacoco to fail. I had a few instrumented jars imported in the classpath which were not required for the junit execution or coverage. These instrumented jars were causing jacoco session to be incorrect as we also had jmockit integration as Mocking Framework. The below link was very helpful for finding out the issue

eclemma.org/jacoco/trunk/doc/classids.html

Upvotes: 1

Related Questions