Java PowerMock ECLemma 覆盖问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23363212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PowerMock ECLEmma coverage issue
提问by venkatesh
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.
我们在 JUnit 中使用 EasyMock 和 PowerMock。使用的覆盖工具是 ECLemma。使用 EasyMock,它以绿色正确显示覆盖范围(如覆盖)。但是,对于使用 PowerMock 进行单元测试的代码,覆盖率显示为红色(未覆盖)。在网上阅读过类似的问题。但是,只是想检查是否有解决方案。
Thanks
Venkatesh
由于
文卡塔斯
采纳答案by Nyamiou The Galeanthrope
It's a known problem : https://github.com/jayway/powermock/issues/422
这是一个已知问题: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 eCoberturainstead.
我建议您改用eCobertura。
回答by Rodrigo Villalba Zayas
Yes, there is a solution for this:
是的,有一个解决方案:
First you will have to add this maven dependency:
首先,您必须添加此 maven 依赖项:
<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:
然后,而不是使用这个注解@RunWith(PowerMockRunner.class),只需在测试类中添加一个@Rule,如下所示:
public class Test {
@Rule
public PowerMockRule rule = new PowerMockRule();
you can find more in this blog Make EclEmma test coverage work with PowerMock
您可以在此博客中找到更多信息Make EclEmma test coverage work with PowerMock
回答by being_ethereal
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:
@RunWith(PowerMockRunner.class)
添加powermock-module-javaagent-1.6.5.jar
到类路径后,删除/注释并包含以下导入:
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:
现在右键单击-> Coverage As-> Coverage Configurations 并在参数中添加以下几行:
-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
.
另请注意,@Before
这在这种情况下不起作用,因此您必须在标记@Test
为 的方法中添加标记为 的方法中的所有内容@Before
。
回答by Pavan Kumar Nyalapalli
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
我们有一个静态类来模拟。使用模拟静态类,eclEmma 代码覆盖插件在 Eclipse 中不起作用。所以我们所做的是,将 @RunWith(JUnit4.class) (而不是 @RunWith(PowerMockRunner.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.
编译类并运行测试类。代码覆盖率适用于课堂。此更改仅在 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.
编写测试用例后,我们将代码恢复正常。放置 @RunWith(PowerMockRunner.class) 而不是 @RunWith(JUnit4.class) 并在静态代码和 powermockrule 行上方注释。
回答by Saran
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.
对于模拟静态类,@RunWith(PowerMockRunner.class)
在 Eclipse 上使用和运行“Coverage As JUnit Test”确实将覆盖的代码显示为未覆盖,这显然是一个问题。
To add to the solutions above, in a maven project, you can try this..
添加到上面的解决方案,在一个maven项目中,你可以试试这个..
In the root pom.xml
, for report generation, add html
as a format in cobertura-maven-plugin
. Below is the way it looks.
在 root 中pom.xml
,对于报告生成,添加html
为cobertura-maven-plugin
. 下面是它的外观。
<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.
然后,转到您的类所在的模块,并target/site/cobertura/index.html
在 Eclipse Web 浏览器或您选择的浏览器中打开文件。您可以在那里找到覆盖信息。
回答by jfcorugedo
I have managed to generate PowerMock coverage with Jacoco, using powermock-module-javaagent
.
我已经设法使用 Jacoco 生成 PowerMock 覆盖范围,使用powermock-module-javaagent
.
Just make sure you put powermock agent afterjacoco agent:
只要确保在 jacoco 代理之后放置 powermock 代理:
<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
如果你想看一个例子,看看这个项目:https: //github.com/jfcorugedo/sonar-scanner
Here you can see that sonar takes into account static methods and new
statements mocked by PowerMock:
在这里你可以看到声纳考虑new
了 PowerMock 模拟的静态方法和语句:
If you want to mock new
statements make sure you use PowerMockRule
instead of PowerMockRunner
.
如果您想模拟new
语句,请确保使用PowerMockRule
而不是PowerMockRunner
.
Take a look at this test
看看这个测试
回答by RBS
Updating powermock version fix my issue below is maven dependency of supported version
更新 powermock 版本修复我下面的问题是支持版本的 maven 依赖
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>1.7.3</version>
<scope>test</scope>
</dependency>
Hope this helps !!!
希望这可以帮助 !!!