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