PowerMockito disables sonar coverage

I use the PowerMockito annotation and @PrepareForTest for my test class. When I do this, Sonar says that none of the branches were covered. However, my other test classes that do not use PowerMockito work well. For example:

@RunWith(PowerMockRunner.class) @PrepareForTest({ MyClass.class }) public class MyClassTest { //create some mocks and run some tests here } 

Is the same problem facing someone?

Thanks in advance.

+7
code-coverage mockito powermock sonarqube
source share
2 answers

There is a known problem with calculating PowerMockito coverage and sonar code, and I did a lot of research on this - there is no reason to do so today. As I personally deal with this - try to avoid using PowerMockito - so use static and final options, which is usually good advice to have more object-oriented code anyway. However, there will still be several cases where you will need to use static and / or final ones. For these, compare my answer here: cobertura-showing-proper-coverage-but-in-sonar-many-files-showing-0-coverage

+6
source share
  • Add cobertura plugin to your sonar server
  • Use the maven cobertura plugin in the assembly (I assume the assembly uses maven)
  • Filing in the properties of cobertura and sonar required to communicate sonar to your server. example as shown.

     <sonar.java.coveragePlugin>cobertura</sonar.java.coveragePlugin> <sonar.host.url>http://localhost:9500</sonar.host.url> <sonar.sources>${project.build.sourceDirectory}</sonar.sources> <sonar.junit.reportsPath>${project.build.directory}/surefire-reports</sonar.junit.reportsPath> <sonar.cobertura.reportPath>${project.build.directory}/site/cobertura/coverage.xml</sonar.cobertura.reportPath> 
  • Run the maven command for sonar i.e. mvn installs the sonar: sonar

+1
source share

All Articles