I have write multiple JUnit test classes for my project.The code covergae is 80% when I
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...