PowerMock ECLEmma coverage issue

女生的网名这么多〃 提交于 2019-12-17 06:47:19

问题



We are using EasyMock and PowerMock with JUnit. The coverage tool used is ECLEmma. With EasyMock, it shows the coverage properly in green (as covered). However, for the code that is unit tested with PowerMock, the coverage is shown in red (uncovered). Have read similar questions on the web. However, just wanted to check if there is a solution for this.

Thanks
Venkatesh


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


I have managed to generate PowerMock coverage with Jacoco, using powermock-module-javaagent.

Just make sure you put powermock agent after jacoco agent:

<artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <useSystemClassLoader>true</useSystemClassLoader>
                <argLine>${jacocoArgLine} -javaagent:${settings.localRepository}/org/powermock/powermock-module-javaagent/${powermock.version}/powermock-module-javaagent-${powermock.version}.jar -noverify</argLine>
...

If you want to see an example, take a look at this project: https://github.com/jfcorugedo/sonar-scanner

Here you can see that sonar takes into account static methods and new statements mocked by PowerMock:

If you want to mock newstatements make sure you use PowerMockRule instead of PowerMockRunner.

Take a look at this test




回答6:


Updating powermock version fix my issue below is maven dependency of supported version

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

Hope this helps !!!




回答7:


For mocking static classes, using @RunWith(PowerMockRunner.class) and running the "Coverage As JUnit Test" on Eclipse does show covered code as uncovered and it clearly does seem like an issue.

To add to the solutions above, in a maven project, you can try this..

In the root pom.xml, for report generation, add html as a format in cobertura-maven-plugin. Below is the way it looks.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <configuration>
       <formats>
          <format>html</format>
          <format>xml</format>
       </formats>
    </configuration>
</plugin>

Then, go to the module where your class resides and open target/site/cobertura/index.html file in Eclipse Web Browser or in the one of your choice. You can find the coverage information there.



来源:https://stackoverflow.com/questions/23363212/powermock-eclemma-coverage-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!