Cobertura Showing proper coverage but In sonar many files showing 0% coverage

随声附和 提交于 2019-12-19 10:33:12

问题


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.

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

Thanks


回答1:


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);

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

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...



来源:https://stackoverflow.com/questions/28087791/cobertura-showing-proper-coverage-but-in-sonar-many-files-showing-0-coverage

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