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

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

问题描述

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

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:

[...]<代码>@嘲笑私有 Logger logMock;[...]<代码>@之前公共无效initMocks()抛出异常{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.

附:附带说明一下,如果您有 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天全站免登陆