我正在使用Powermock,但是当我运行Eclemma coverage时,由于EclEmma的已知问题,最终分数中未考虑Powermock测试用例。
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.
为此,我搜索的结果以及关于stackoverflow的其他答案表明,解决方法如下:
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(); }然后添加jar,例如powermock-module-javaagent,powermock-module-junit4-规则代理。
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此答案建议具有各种版本的javassit。但是我无法完成这项工作并遇到相同的错误。
For this answers suggest to have javassit of various versions. But I am unable to get that work and getting the same error.
我的pom.xml如下:
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>我如何才能将PowerMock测试用例包含在Eclemma的最终分数中?
How can I get PowerMock test cases to be included in final Eclemma score ?
推荐答案我最喜欢的非答案:不要浪费时间尝试。
My favorite non-answer: don't waste your time trying.
相反,将此作为另一个提示,即不应使用PowerMock。并花费您的时间和精力来学习如何编写更好的生产代码,而无需使用PowerMock即可对其进行测试。
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.
例如,开始研究那些视频;了解避免使用静态或 new (以无法测试的方式)调用的代码通常是 bad 代码;改进此代码不仅可以帮助您摆脱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.
如果您发现将PowerMock置于困难之中,那么,我的建议:然后将测试放入两个存储桶中(一个用于将PowerMock真正用于只有PowerMock可以做的事情的测试;另一个用于所有其他测试)。并且仅测量下一个的覆盖率。这也会打开一条前进的道路:只需避免对您的任何新事物使用PowerMock。取而代之的是,您可以求助于Mockito,这在最近几年中已经有所改进。甚至允许您(实验性地)像覆盖最终方法那样。
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.
更多推荐
EclEmma,Powermock和Java 7问题
发布评论