2

I am using Powermock but when I run Eclemma coverage, the Powermock test cases are not considered in the final score because of known issues with EclEmma.

The work around for this which I search and other answers on stackoverflow suggest to have:

@Rule
public PowerMockRule rule = new PowerMockRule();

static {
 PowerMockAgent.initializeIfNeeded();
}

Then add jars like powermock-module-javaagent, powermock-module-junit4-rule-agent.

After doing this when I am running my code then finding error:

java.lang.VerifyError: Expecting a stackmap frame at branch target 7

For this answers suggest to have javassit of various versions. But I am unable to get that work and getting the same error.

My pom.xml looks like:

<dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.6.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.6.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-javaagent</artifactId>
        <version>1.6.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-junit4-rule-agent</artifactId>
      <version>1.6.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.21.0-GA</version>
      <scope>test</scope>
    </dependency>

How can I get PowerMock test cases to be included in final Eclemma score ?

GhostCat
  • 137,827
  • 25
  • 176
  • 248

2 Answers2

3

My favorite non-answer: don't waste your time trying.

Instead, take this as another hint that PowerMock should not be used; and use your time and energy to learn how to write better production code that can be tested without the need to PowerMock.

For example, start looking into those videos; to understand that code that avoids calls to static or new (in an untestable) way is very often bad code; and that improving this code not only helps you to get rid of PowerMock, but also to improve your over all product.

And in case you find leaving PowerMock to hard; my suggestion: then put your tests into two buckets (one for those that really use PowerMock for things only PowerMock can do; and one for all others); and only measure coverage for the later one. This would also open a "path forward": simply avoid using PowerMock for any new things you. Instead, you could turn to mockito, which has improved a lot of the last years; even allowing you things (experimentally) like overriding final methods.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

I figured out a way, may be not clean but I think it works out.
So the problem statement was how to get Powermock get included in EclEmma ?.
The first change in approach was move to maven approach of Eclemma i.e. jacoco
Please feel free to go through the link below before moving to the answer:

Power mock with jacoco
Basic of Java Instrumentation

In a nutshell, jacoco identifies code coverage by having a lock/updating bytecode/instrumenting the class, when being loaded.
And powermock for its working also lock/updates bytecode/instrumenting the class for its functioning, BUT IT DOES THIS FROM THE DISK AND NOT THE MEMORY.
Thus when coverage is given by jacoco, powermock classes are not covered as powermock runs test cases from disk files and jacoco has no idea what powermock is doing.
So the work around it to store the classes instrumented by jacoco on the disk and then powermock runs test through those classes which in turn will let jacoco know that test are running and include them in code coverage.
[PLS FEEL FREE TO CORRECT MY UNDERSTANDING REGARDING THIS]
So the final code changes are:

    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>org.jacoco.agent</artifactId>
        <version>0.7.7.201606060606</version>
        <classifier>runtime</classifier>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.6.1</version>
        <scope>test</scope>
    </dependency>

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <configuration>
                <excludes>
                 // files you want to exclude
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <id>default-instrument</id>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-restore-instrumented-classes</id>
                    <goals>
                        <goal>restore-instrumented-classes</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${project.build.directory}/coverage.exec</dataFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <systemPropertyVariables>
                    <jacoco-agent.destfile>${project.build.directory}/coverage.exec</jacoco-agent.destfile>
                </systemPropertyVariables>
            </configuration>
        </plugin>
    </plugins>
</build>

after this running -PcodeCoverage worked out for me.