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

后端 未结 1 541
天涯浪人
天涯浪人 2021-01-14 22:52

\"enterI have write multiple JUnit test classes for my project.The code covergae is 80% when I

相关标签:
1条回答
  • 2021-01-14 23:47

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

    0 讨论(0)
提交回复
热议问题