Ant : Jacoco Code Coverage Report is not generating with PowerMocito

2,138 views
Skip to first unread message

vijay kumar

unread,
Sep 21, 2016, 9:13:27 AM9/21/16
to JaCoCo and EclEmma Users
Hi, We have integrated jacoco with our ant project and code coverage report is not generated for the classes in which we have used @RunWith(PowerMockRunner.class) (Powermock-mockito-1.5-full.jar)

Request you to refer the below mentioned build.xml configuration details :

Please inform is there any changes which we need to do in build.xml file or need to do changes in our junit test files.

<!-- jacoco -->

<taskdef uri="org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">

        <classpath path="${basedir}/${ivy.lib}/jacoco-0.7.6/lib/jacocoant.jar"/>

  </taskdef>


<target name="unit_test" depends="unit_test_compile"

        description="Run junit tests">

    <jacoco:coverage destfile="${coverage.dir}/jacoco.exec" xmlns:jacoco="org.jacoco.ant">

<junit fork="yes" forkmode="perBatch" haltonfailure="yes" printsummary="true">

<classpath refid="unit.test.classpath" />  <!-- classpath contains nessary jar files for junit and powermockito -->

<formatter type="xml"/>

<batchtest fork="yes" todir="${unit.test.reports}">

<fileset dir="${unit.test.src}">

<include name="**/*Test*.java"/>

<exclude name="**/AllTests.java"/>

<exclude name="**/TestHelpers.java"/>

</fileset>

</batchtest>

</junit>

    </jacoco:coverage>

</target>

<target name="coverage-report" depends="unit_test">

<jacoco:report xmlns:jacoco="org.jacoco.ant">

        <executiondata>

            <file file="${coverage.dir}/jacoco.exec" />

        </executiondata>


        <structure name="Jacoco">

            <classfiles>

                <fileset dir="${bin}" />

            </classfiles>

            <sourcefiles encoding="UTF-8">

                <fileset dir="${src}" />

            </sourcefiles>

        </structure>


        <html destdir="${coverage.report.dir}" />

    </jacoco:report>

</target>

Marc Hoffmann

unread,
Sep 21, 2016, 10:06:54 AM9/21/16
to jac...@googlegroups.com
Hi,

the JaCoCo agent does not work with frameworks like Powermock which
modify the classes under test. Please find a detailed discussion and
possible workarounds here:

http://www.eclemma.org/jacoco/trunk/doc/classids.html

Regards,
-marc
> --
> You received this message because you are subscribed to the Google
> Groups "JaCoCo and EclEmma Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to jacoco+un...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jacoco/7d2e0d22-6be6-43d4-afe5-e8115d0ecc81%40googlegroups.com
> [1].
> For more options, visit https://groups.google.com/d/optout.
>
>
> Links:
> ------
> [1]
> https://groups.google.com/d/msgid/jacoco/7d2e0d22-6be6-43d4-afe5-e8115d0ecc81%40googlegroups.com?utm_medium=email&utm_source=footer

vijay kumar

unread,
Sep 22, 2016, 6:12:49 AM9/22/16
to JaCoCo and EclEmma Users
Hi,



Workaround i can see is 
  • Use offline instrumentation before you run your tests. This way classes get instrumented by JaCoCo before any runtime modification can take place. Note that in this case the report has to be generated with the original classes, not with instrumented ones.
Please share us the example in which offline instrumentation is implemented with Ant with reference to my implementation which i shared.

vijay kumar

unread,
Sep 22, 2016, 1:50:13 PM9/22/16
to JaCoCo and EclEmma Users
Hi Marc, In another post i can see jacoco supports with powerMock.


Request you to share me working example which we have with ant.

Evgeny Mandrikov

unread,
Sep 24, 2016, 4:00:25 PM9/24/16
to JaCoCo and EclEmma Users
Excuse me, but is it that hard to find and read documentation and apply learned material for your needs?

http://www.eclemma.org/jacoco/trunk/doc/offline.html


And BTW there is already an example


http://www.eclemma.org/jacoco/trunk/doc/



If something will still be unclear, the please post more specific question.


Thank you for your understanding.

vijay kumar

unread,
Sep 28, 2016, 6:44:57 AM9/28/16
to JaCoCo and EclEmma Users
Thanks for details.

I have gone through these documents but i didn't find any unit test files are ran using junit in the ant offline instrumentation example.

Also when i ran the below mentioned build.xml (which is from offline instrumentation) it's not working and i am getting the error message as 

compile:

    [mkdir] Created dir: /Users/gvijayakuma2/code/src/vsphere/com.csc.agility.adapters.cloud.vsphere/target/classes

    [javac] Compiling 31 source files to /Users/gvijayakuma2/code/src/vsphere/com.csc.agility.adapters.cloud.vsphere/target/classes

    [javac] Note: Some input files use or override a deprecated API.

    [javac] Note: Recompile with -Xlint:deprecation for details.

    [javac] Note: Some input files use unchecked or unsafe operations.

    [javac] Note: Recompile with -Xlint:unchecked for details.


instrument:

[jacoco:instrument] Instrumented 72 classes to /Users/gvijayakuma2/code/src/vsphere/com.csc.agility.adapters.cloud.vsphere/target/classes-instr


test:

     [java] Error: Could not find or load main class org.jacoco.examples.parser.Main

     [java] Java Result: 1


report:

[jacoco:report] Loading execution data file /Users/gvijayakuma2/code/src/vsphere/com.csc.agility.adapters.cloud.vsphere/target/jacoco.exec



Request you to share me the example for instrumentation of classes, unit test using junit for ant build.xml which uses powerMock


<project name="com.csc.agility.adapters.cloud.vsphere" basedir="." default="help" xmlns:ivy="antlib:org.apache.ivy.ant">


<!-- =====================================================================

    Definitions

    ================================================================= -->

<!-- version properties -->


<property name="version.major" value="2" />

<property name="version.minor" value="0" />

<property name="version.build" value="0" />

<property name="bundle.version" value="${version.major}.${version.minor}.${version.build}" />

<property name="rpm.version" value="${version.major}.${version.minor}-${version.build}"/>

<property name="src.dir" location="./src"/>

<property name="result.dir" location="./target"/>

<property name="result.classes.dir" location="${result.dir}/classes"/>

<property name="result.classes.instr.dir" location="${result.dir}/classes-instr"/>

<property name="result.report.dir" location="${result.dir}/site/jacoco"/>

<property name="result.exec.file" location="${result.dir}/jacoco.exec"/>



<path id="compile.classpath">

<fileset dir="./lib" includes="**/*.jar"/>

<fileset dir="./ivy-lib">

<include name="com.servicemesh.agility.api-*.jar" />

<include name="com.servicemesh.agility.api.script-*.jar" />

<include name="com.servicemesh.agility.sdk.cloud-*.jar" />

<include name="com.servicemesh.core-*.jar" />

<include name="com.servicemesh.io-*.jar" />

<include name="log4j-*.jar" />

<include name="osgi-*.jar" />

<include name="/jacoco-0.7.6/**.*.jar" />

</fileset>

</path>

<!--  Step 1: Import JaCoCo Ant tasks  -->

<taskdef uri="org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">

<classpath path="../../../lib/jacocoant.jar"/>

<classpath path="../../../lib/jacocoagent.jar"/>

</taskdef>

<target name="clean">

<delete dir="${result.dir}"/>

</target>

<target name="compile">

<mkdir dir="${result.classes.dir}"/>

<javac srcdir="${src.dir}" destdir="${result.classes.dir}" debug="true" includeantruntime="false">

<classpath>

<path refid="compile.classpath"/>

</classpath>

</javac>

</target>

<target name="instrument" depends="compile">

<!--  Step 2: Instrument class files  -->

<jacoco:instrument  destdir="${result.classes.instr.dir}" xmlns:jacoco="org.jacoco.ant">

<fileset dir="${result.classes.dir}"/>

</jacoco:instrument>

</target>

<target name="test" depends="instrument">

<!--  Step 3: Run tests with instrumented classes  -->

<java classname="org.jacoco.examples.parser.Main" fork="true">

<!--  jacocoagent.jar must be on the classpath  -->

<classpath>


<pathelement path="./ivy-lib/jacoco-0.7.6/lib/jacocoagent.jar"/>

<pathelement path="./ivy-lib/jacoco-0.7.6/lib/org.jacoco.ant-0.7.6.201602180812.jar"/>

<pathelement path="./ivy-lib/jacoco-0.7.6/lib/**.*jar"/>


<pathelement path="${result.classes.instr.dir}"/>

</classpath>

<!--  Agent is configured with system properties  -->

<sysproperty key="jacoco-agent.destfile" file="${result.exec.file}"/>

<arg value="2 * 3 + 4"/>

<arg value="2 + 3 * 4"/>

<arg value="(2 + 3) * 4"/>

<arg value="2 * 2 * 2 * 2"/>

<arg value="1 + 2 + 3 + 4"/>

<arg value="2 * 3 + 2 * 5"/>

</java>

</target>

<target name="report" depends="test">

<!--  Step 4: Create coverage report  -->

<jacoco:report xmlns:jacoco="org.jacoco.ant">

<!--

 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="${result.classes.dir}"/>

</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="clean,compile,instrument,test,report"/>

</project>

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages