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

旧巷老猫 提交于 2019-12-01 11:13:45

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

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