简体   繁体   English

为什么EclEmma没有使用@RunWith(PowerMockRunner.class)进行测试的覆盖代码

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

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. 我正在使用EclEmma和Eclipse来帮助我知道我的项目中缺少代码测试的地方,但所有使用@RunWith(PowerMockRunner.class)测试都没有被调用,因此没有经过测试。

I´m using JUnit 4.8.1 with Mockito. 我正在使用JUnit 4.8.1和Mockito。

What could it be? 会是什么呢?

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 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. 然而, eCoberture似乎提供了正确的报道。 The only problem, that it seems not to be maintained anymore, and you cannot remove the highlights im Eclipse Juno. 唯一的问题,它似乎不再被维护,你无法删除Eclipse Juno的亮点。

Here you can find example that works and may help you solve this problem https://github.com/Godin/jacoco-experiments 在这里你可以找到有效的例子,可以帮助你解决这个问题https://github.com/Godin/jacoco-experiments

use mvn clean package to see jacoco report 使用mvn clean package来查看jacoco报告

We have a static classes to mock. 我们有一个静态类来模拟。 With mocking static classes, eclEmma code coverage plugin is not working in Eclipse. 使用模拟静态类,eclEmma代码覆盖插件在Eclipse中不起作用。 So what we did is, so placed @RunWith(JUnit4.class) (Instead of @RunWith(PowerMockRunner.class) ) before class and placed following lines inside class 所以我们所做的是,在课堂上放置@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 fine for class. 代码覆盖率适用于课堂。 This change is only to run eclEmma plugin in Eclipse IDE with no issues. 此更改仅用于在Eclipse IDE中运行eclEmma插件,没有任何问题。

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行。

AFAIK eclEmma, as well as many other coverage systems, modify your .class files to add coverage instructions. AFAIK eclEmma以及许多其他覆盖系统会修改.class文件以添加覆盖指令。 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以及AspectJ LTW和许多其他系统在“运行时”操作相同的字节码:

PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock是一个框架,可以扩展其他模拟库,例如EasyMock,具有更强大的功能。 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. PowerMock使用自定义类加载器和字节码操作来实现静态方法,构造函数,最终类和方法的模拟,私有方法,静态初始化程序的删除等。

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. 我对eclEmma(各种版本)和Cobertura与AspectJ LTW结合都有类似的问题,因为当.class文件的运行时修改发生时,它会以某种方式破坏覆盖工具之前完成的修改。

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. 正确的解决方案是调试PowerMock仪器并找出它在何处以及如何打破覆盖工具。 It's quite a problem, for a testing tool, to break coverage tools, since the two are quite often used together :) 对于测试工具来说,打破覆盖工具是一个很大的问题,因为两者经常在一起使用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 问题在于@RunWith(PowerMockRunner.class) - the problem is with @RunWith(PowerMockRunner.class) 没有@RunWith的模拟静态(PowerMockRunner.class) - mock static without @RunWith(PowerMockRunner.class) 使用 @RunWith(PowerMockRunner.class) 时出现 NoClassDefFoundError - NoClassDefFoundError when using @RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)vs @RunWith(MockitoJUnitRunner.class) - @RunWith(PowerMockRunner.class) vs @RunWith(MockitoJUnitRunner.class) @RunWith(PowerMockRunner.class)说@RunWith不适用于方法 - @RunWith(PowerMockRunner.class) says @RunWith not applicable to method 使用@RunWith(PowerMockRunner.class)进行Junit测试失败 - “没有找到匹配的测试......” - Junit test with @RunWith(PowerMockRunner.class) fails - “No tests found matching..” @RunWith(PowerMockRunner.class)与tycho surefire不兼容 - @RunWith(PowerMockRunner.class) do not work with tycho surefire RunWith(PowerMockRunner.class)不适用于包注释 - RunWith(PowerMockRunner.class) does not work with package annotation PowerMock从@RunWith(PowerMockRunner.class)批注引发异常 - PowerMock Throwing an Exception from @RunWith(PowerMockRunner.class) Annotation 带有@RunWith(PowerMockRunner.class)批注的测试类在扩展另一个类时两次运行测试用例 - Test Class with @RunWith(PowerMockRunner.class) annotation runs test cases twice when extending another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM