Sonar does not shows up Code Coverage after build successful with Jenkins Sonar plugin

code-coveragesonarqube

I am trying to get code coverage with Sonar and Jenkins. I see Jenkins' Sonar plugin successfully executes JUnit test cases and completes build successfully. But Sonar does not show Code Coverage results (always shows 0.0% as the code coverage) on the project. But Sonar does show "Unit test success".

I am using Maven with Jenkins and Sonar.

I get the below message in Jenkins logs while executing the Sonar plugin:

Project coverage is set to 0% as no JaCoCo execution data has been dumped: .../sonar/target/jacoco.exec

Can any one help me how to get correct code coverage on any Sonar project.

Best Answer

Just because Sonar invoked Surefire correctly (and you received the "Unit test success" message) doesn't mean that JaCoCo instrumented your code.

Try executing JaCoCo directly. You might find out why JaCoCo is failing directly:

mvn jacoco:prepare-agent test jacoco:report

JaCoCo will place jacoco.exec as well as its XML/HTML reports within target/jacoco. Or it will fail, and hopefully you'll have a better idea as to why.

One very common problem is that the JaCoCo javaagent will not run if you've changed the Surefire argLine at all, because jacoco:prepare-agent just sets the argLine property which in this scenario, is conveniently ignored. You can set prepare-agent's propertyName property to something else (like jacocoArgLine) and include that in your argLine config:

<argLine>-Xmx1024m ${jacocoArgLine}</argLine>

Related Topic