PowerMock ECLEmma coverage issue

It's a known problem : https://github.com/jayway/powermock/issues/422

And it has been for a long time, it won't be fixed anytime soon.

I suggest you use eCobertura instead.


Yes, there is a solution for this:

First you will have to add this maven dependency:

<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-module-junit4-rule-agent</artifactId>
  <version>1.6.4</version>
  <scope>test</scope>
</dependency>

Then, instead of using this annotation @RunWith(PowerMockRunner.class), just add a @Rule in the Test class like this:

public class Test {

   @Rule
   public PowerMockRule rule = new PowerMockRule();

you can find more in this blog Make EclEmma test coverage work with PowerMock


We have a static classes to mock. With mocking static classes, eclEmma code coverage plugin is not working in Eclipse. So what we did is, so placed @RunWith(JUnit4.class) (Instead of @RunWith(PowerMockRunner.class) ) before class and placed following lines inside class

static {
PowerMockAgent.initializeIfNeeded();
}

@Rule
public PowerMockRule rule = new PowerMockRule();

Compiled the class and ran the test class. Code coverage is working for class. This change is only in Eclipse IDE.

After writing test cases, we reverted code back to normal. Placed @RunWith(PowerMockRunner.class) instead of @RunWith(JUnit4.class) and commented above static code and powermockrule lines.


This has worked in most cases in my project:

@Rule
public PowerMockRule rule = new PowerMockRule();
static {
    PowerMockAgent.initializeIfNeeded();
}

Remove/Comment @RunWith(PowerMockRunner.class) & include following imports after adding powermock-module-javaagent-1.6.5.jar in your classpath:

import org.junit.Rule;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.powermock.modules.agent.PowerMockAgent;

Now right click->Coverage As->Coverage Configurations and add following lines in Arguments:

-ea -noverify -javaagent:path/to/powermock-module-javaagent-1.6.5.jar

Click Apply->Coverage.

Also note that @Before would not work in this case so you have to add all the stuffs in the methods marked with @Test from the method marked with @Before.