We want to use JACOCO together with Powermock. I tested with a simple example, in which powermock is used to mock a function-call. In the build-offline.xml-example the coverage is triggered by the main-function, I need the coverage triggered by the unit-test. Therefore I modified the ANT-script in such a way, that the coverage is triggered by the Unittest. But Powermock overwrites the information. Can you tell me, if there is something wrong in my ANT-script.
build-skript: ============= <?xml version="1.0" encoding="UTF-8"?> <!-- Copyright (c) 2009, 2017 Mountainminds GmbH & Co. KG and Contributors All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Marc R. Hoffmann - initial API and implementation --> <project name="Example Ant Build with JaCoCo" default="rebuild" xmlns:jacoco="antlib:org.jacoco.ant"> <description> Example Ant build file that demonstrates how a JaCoCo coverage report can be itegrated into an existing build in three simple steps. </description> <property name="src.dir" location="./src/main/java" /> <property name="test.dir" location="./src/test/java" /> <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="classpath.test"> <pathelement location="../../tools/junit/junit.jar"/> <pathelement location="../../tools/junit/org.hamcrest.core.jar"/> <pathelement location="../../tools/powermock/cglib-nodep-2.2.2.jar"/> <pathelement location="../../tools/powermock/javassist-3.21.0-GA.jar"/> <pathelement location="../../tools/powermock/mockito-core-1.10.19.jar"/> <pathelement location="../../tools/powermock/objenesis-2.4.jar"/> <pathelement location="../../tools/powermock/powermock-mockito-1.7.0-full.jar"/> <pathelement location="${result.classes.dir}" /> </path> <path id="classpath.instr.test"> <pathelement location="../../tools/junit/junit.jar"/> <pathelement location="../../tools/junit/org.hamcrest.core.jar"/> <pathelement location="../../tools/powermock/cglib-nodep-2.2.2.jar"/> <pathelement location="../../tools/powermock/javassist-3.21.0-GA.jar"/> <pathelement location="../../tools/powermock/mockito-core-1.10.19.jar"/> <pathelement location="../../tools/powermock/objenesis-2.4.jar"/> <pathelement location="../../tools/powermock/powermock-mockito-1.7.0-full.jar"/> <pathelement location="d:/tools/jacococo/lib/jacocoagent.jar"/> <pathelement location="${result.classes.instr.dir}" /> </path> <!-- Step 1: Import JaCoCo Ant tasks --> <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml"> <classpath path="../../tools/jacococo/lib/jacocoant.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" /> </target> <target name="test-compile" depends="compile"> <javac srcdir="${test.dir}" destdir="${result.classes.dir}" debug="true" includeantruntime="false"> <classpath refid="classpath.test"/> </javac> </target> <target name="instrument" depends="test-compile"> <!-- Step 2: Instrument class files --> <jacoco:instrument destdir="${result.classes.instr.dir}"> <fileset dir="${result.classes.dir}" /> </jacoco:instrument> </target> <target name="unittest" depends="instrument"> <junit printsummary="yes" haltonfailure="yes" fork="true"> <sysproperty key="jacoco-agent.destfile" file="${result.exec.file}"/> <classpath refid="classpath.instr.test"/> <test name="javascen.testscript.PowermockCalcTest" haltonfailure="no" outfile="result"> <formatter type="plain"/> <formatter type="xml"/> </test> </junit> </target> <target name="report" depends="unittest"> <!-- 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="${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, test-compile, instrument, unittest, report" /> </project> Classes used: ============= public class Calculate { public int add(int a, int b) { return (a + b); } public int sub(int a, int b) { return (a - b); } public int mult(int a, int b) { return (a * b); } } public class CalculateMain { public int compute(int a, int b) { Calculate calc = new Calculate(); int result = calc.add(1, 2); return (result); } } public class PowermockCalcTest { @Mock private CalculateMain calculateMain = null; @Before public void beforeEachTest() { MockitoAnnotations.initMocks(this); calculateMain = PowerMockito.mock(CalculateMain.class, new CallsRealMethods()); } @Test public void test() { PowerMockito.doReturn(6).when(calculateMain).compute(1, 2); assertEquals("Bad value", 6, calculateMain.compute(1, 2)); } } Thank you Peter -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/29b8543c-5cdc-469f-b739-181d924c695d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
