Why EclEmma doesn't coverage code with tests with @RunWith(PowerMockRunner.class)

烂漫一生 提交于 2019-12-21 04:03:26

问题


I am using EclEmma with Eclipse to help me to know where is missing code tests in my project, but all tests with @RunWith(PowerMockRunner.class) are not called and thus not tested.

I´m using JUnit 4.8.1 with Mockito.

What could it be?


回答1:


Its a known bug reported for both parties:

http://code.google.com/p/powermock/issues/detail?id=402 https://github.com/jacoco/eclemma/issues/15#issuecomment-9565210

eCoberture seems however to provide correct coverage. The only problem, that it seems not to be maintained anymore, and you cannot remove the highlights im Eclipse Juno.




回答2:


Here you can find example that works and may help you solve this problem https://github.com/Godin/jacoco-experiments

use mvn clean package to see jacoco report




回答3:


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 fine for class. This change is only to run eclEmma plugin in Eclipse IDE with no issues.

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.




回答4:


AFAIK eclEmma, as well as many other coverage systems, modify your .class files to add coverage instructions. Most of these tools do that at "compile time", not at run time.

PowerMock instead, as well as AspectJ LTW and many other systems, manipulate the same bytecode but at "run time":

PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more.

I have a similar problem with both eclEmma (various versions) and Cobertura in combination with AspectJ LTW, cause when the runtime modification of .class files happen, it somehow corrupts the modification done previously by the coverage tool.

I don't have yet found a solution, but at least found the symptom.

The right solution would be to debug PowerMock instrumentation and find out where and how it breaks coverage tools. It's quite a problem, for a testing tool, to break coverage tools, since the two are quite often used together :)



来源:https://stackoverflow.com/questions/9726212/why-eclemma-doesnt-coverage-code-with-tests-with-runwithpowermockrunner-class

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