Problems with Jacoco and Powermock

121 views
Skip to first unread message

peter_...@web.de

unread,
Dec 7, 2017, 7:35:06 AM12/7/17
to JaCoCo and EclEmma Users
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

Evgeny Mandrikov

unread,
Dec 7, 2017, 8:39:04 AM12/7/17
to JaCoCo and EclEmma Users
It is quite hard to examine/experiment with your code because it is quite hard to execute as it requires first to download JARs, etc... Please provide full, complete and easily runnable example, e.g. by archiving everything into zip-archive or by placing in public GitHub repository.

peter_...@web.de

unread,
Dec 7, 2017, 10:46:12 AM12/7/17
to JaCoCo and EclEmma Users
Hi,

I am not able to attach the ZIP-file. The GitHub repository is blocked in our company. Is there another possibility to send the ZIP-file to you.

Peter

Evgeny Mandrikov

unread,
Dec 7, 2017, 11:33:45 AM12/7/17
to JaCoCo and EclEmma Users


On Thursday, December 7, 2017 at 4:46:12 PM UTC+1, peter_...@web.de wrote:
I am not able to attach the ZIP-file. The GitHub repository is blocked in our company. Is there another possibility to send the ZIP-file to you. 

dropbox, google drive, any way of sharing of your choice will do the job
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages