Cobertura显示适当的覆盖范围,但在声纳中,许多文件显示0%的覆盖范围 [英] Cobertura Showing proper coverage but In sonar many files showing 0% coverage

查看:183
本文介绍了Cobertura显示适当的覆盖范围,但在声纳中,许多文件显示0%的覆盖范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的项目编写了多个JUnit测试类.代码覆盖率是80%当我在Eclipse中使用cobertura插件看到它时,但是当我尝试在Sonar中看到我的代码覆盖率时,它只显示了35%,其背后的原因是多个类的覆盖率为0%,而某些类显示了覆盖率.我不知道.是声纳问题还是我的代码存在问题,因为我在某个地方使用EasyMock和Mockito的PowerMockito.

I have write multiple JUnit test classes for my project.The code covergae is 80% when I see it in Eclipse using cobertura plugin.But when I try to see my code coverage in Sonar it show only 35%.The reason behind this is that multiple classes have 0% coverage and some classes shows coverage.What is the main reason I don't know.Is it problem of sonar or there is some problem im my code beacuse somewhere I am using PowerMockito somewhere EasyMock and somewhere Mockito.

我将同时附上Cobertura所展示的报道和Sonar所展示的报道的快照. 请帮助我.

I am attaching the snapshots of both the coverage one shown by cobertura and one shown by Sonar. Kindly help me.

谢谢

推荐答案

PowerMockito和代码覆盖率计算存在一个已知问题.无论如何,应该稀疏使用PowerMockito. Mockito不提供PowerMockito提供的功能的原因,主要是Mockito试图使您专注于良好的,可测试的代码(而静态和最终代码则不是). 在少数使用PowerMockito且代码覆盖率计算不正确的地方,我编写了一个Reflection Util类,使我可以从属性中删除static和final.完成之后,我可以像常规实例属性一样模拟属性,并正确计算代码覆盖率.我对静态的最终Logger日志属性执行此操作,例如:

There is a know issue with PowerMockito and code coverage computation. PowerMockito should be used sparsely anyway. The reason Mockito doesn't offer the functionality PowerMockito does offer, is mostly that Mockito tries to make you focus on good, testable code (which static and final is not). In the few places where I used PowerMockito and the code coverage was not calculated correctly, I have programmed a little Reflection Util class that would allow me to remove static and final from attributes. After having done that, I can mock the attributes just like regular instance attributes and the code coverage is calculated correctly. I do this for static final Logger log attributes, for instance, like this:

[...] @Mock private Logger logMock; [...] @Before public void initMocks() throws Exception { MockitoAnnotations.initMocks(this); [...] ReflectionUtils.setFinalStatic(MyClass.class.getDeclaredField("LOG"), logMock);

[...] @Mock private Logger logMock; [...] @Before public void initMocks() throws Exception { MockitoAnnotations.initMocks(this); [...] ReflectionUtils.setFinalStatic(MyClass.class.getDeclaredField("LOG"), logMock);

我无法在此处发布ReflectionUtils类的代码,但可以在网上轻松找到示例.

The Code of the ReflectionUtils class I cannot post here, but examples can be easily found online.

P.s.顺便说一句,如果您的差距在80%到35%之间,这意味着您有45%的代码是静态的或最终的代码,就我个人而言,您的代码似乎存在很大的设计缺陷,应在解决之前进行修复在Sonar中调整代码覆盖率度量值...

P.s. On a side note, if you have a gap of 80% to 35%, meaning you have 45% code that is static and or final, it seems to me personally you have a big design flaw with your code, that you should fix before tweaking your code coverage measurements in Sonar...

这篇关于Cobertura显示适当的覆盖范围,但在声纳中,许多文件显示0%的覆盖范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆