Cobertura report not increasing the code coverage with Powermock

1.1k Views Asked by At

I am using Cobertura maven plugin [version 2.7] to understand the code coverage for my tests. I am using PowerMock [version 1.6.6] to mock dependent objects. But when I run mvn cobertura:cobertura and check the report, the coverage remains the same. It works fine when I do not use mocking. Is this a compatibility issue? I tried mvn clean multiple times just to be sure that the report is newly generated.

Here is my pom.xml

<properties>
    <powermock.version>1.6.6</powermock.version>
</properties>
<build>
    <plugins>
        <!-- Cobertura plugin for code coverage -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <formats>
                    <format>html</format>
                    <format>xml</format>
                </formats>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>${powermock.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>${powermock.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Any help on this is appreciated.

1

There are 1 best solutions below

0
On

Unfortunately, there's a big chance that it's impossible. I don't know how exactly Cobertura works, but I suspect that it uses same approach like a JaCoCo and modifies byte code with Java Agent. But PowerMock reads classes from disk when loads a class, so all changes are lost. One small change that Cobertura can modify classes during compile time. If yes, then you may try it.

Cobertura project looks abandoned, so I don't see any reason to spend time on integrating with not supported project. I'd like to focus on integration with JaCoCo and provides supporting on-fly instrumenting.