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 显示的覆盖范围快照。
请帮助我。
Thanks
PowerMockito 和代码覆盖率计算存在一个已知问题。无论如何,PowerMockito 应该很少使用。 Mockito 不提供 PowerMockito 提供的功能的原因主要是 Mockito 试图让您专注于良好的、可测试的代码(静态和最终代码不是)。
在我使用 PowerMockito 并且代码覆盖率计算不正确的几个地方,我编写了一个小 Reflection Util 类,它允许我从属性中删除 static 和 final 。完成此操作后,我可以像常规实例属性一样模拟属性,并且可以正确计算代码覆盖率。我对静态最终 Logger 日志属性执行此操作,例如,如下所示:
[...]
@Mock
private Logger logMock;
[...]@Before
public void initMocks() throws Exception {
MockitoAnnotations.initMocks(this);
[...]
ReflectionUtils.setFinalStatic(MyClass.class.getDeclaredField("LOG"), logMock);
ReflectionUtils 类的代码我无法在这里发布,但可以在网上轻松找到示例。
附注顺便说一句,如果你有 80% 到 35% 的差距,这意味着你有 45% 的代码是静态的和/或最终的,在我个人看来,你的代码有一个很大的设计缺陷,你应该在之前修复它调整 Sonar 中的代码覆盖率测量...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)