[Java 2.1.x] Sonar coverage

838 views
Skip to first unread message

Peter Önneby

unread,
Jul 19, 2013, 7:02:02 AM7/19/13
to play-fr...@googlegroups.com
Hi,

Has anyone managed to get Sonar to analyse the coverage data (jacoco.exec)? 
I get a NullpointerException when using Sonar runner and sonar.jacoco.reportPath=target/scala-2.10/jacoco/jacoco.exec;

Exception in thread "main" org.sonar.runner.RunnerException: java.lang.NullPointerException
        at org.sonar.runner.Runner.delegateExecution(Runner.java:288)
        at org.sonar.runner.Runner.execute(Runner.java:151)
        at org.sonar.runner.Main.execute(Main.java:84)
        at org.sonar.runner.Main.main(Main.java:56)
Caused by: java.lang.NullPointerException
        at org.sonar.plugins.jacoco.ExecutionDataVisitor.visitClassExecution(ExecutionDataVisitor.java:48)
        at org.jacoco.core.data.ExecutionDataReader.readExecutionData(ExecutionDataReader.java:147)
        at org.jacoco.core.data.ExecutionDataReader.readBlock(ExecutionDataReader.java:111)
        at org.jacoco.core.data.ExecutionDataReader.read(ExecutionDataReader.java:85)
        at org.sonar.plugins.jacoco.AbstractAnalyzer.readExecutionData(AbstractAnalyzer.java:118)
...

Plugins.scala

addSbtPlugin("play" % "sbt-plugin" % "2.1.1")

addSbtPlugin
("de.johoop" % "jacoco4sbt" % "1.2.4")



My Build.scala:

lazy val s = Defaults.defaultSettings ++ Seq(jacoco.settings:_*)


  val main
= play.Project(appName, appVersion, appDependencies, settings = s).settings(
   
// Add your own project settings here      
    parallelExecution    
in jacoco.Config := false,
    jacoco
.reportFormats  in jacoco.Config := Seq(XMLReport("utf-8"), HTMLReport("utf-8")),
    jacoco
.excludes       in jacoco.Config := Seq("*anon*", "views*", "*Routes*", "controllers*routes*", "controllers*Reverse*", "controllers*javascript*", "controller*ref*")
 
)

Does anyone have a working setup?

Thanks
Peter

Philip Johnson

unread,
Jul 19, 2013, 12:12:37 PM7/19/13
to play-fr...@googlegroups.com
Hi Peter,

Have you tested to make sure you have Jacoco working in a standalone fashion yet?  If not, you can use this project for sample code:


Philip

Peter Önneby

unread,
Jul 19, 2013, 5:13:31 PM7/19/13
to play-fr...@googlegroups.com
Hi Philip,

Thanks for the link, I forgot to say I do already get the HTML report OK.
It's as if there is something missing in the generated jacoco.exec.
Any other ideas before I start debugging the sonar sources?

Peter

Łukasz M.

unread,
Aug 29, 2013, 8:07:38 AM8/29/13
to play-fr...@googlegroups.com
Hey Peter,

Have you managed to get SonarQube to work with Play Jacoco?

I'm struggling with the same issue

Thank Łukasz

Roch Delsalle

unread,
Aug 29, 2013, 9:26:12 AM8/29/13
to play-fr...@googlegroups.com
I'm also trying to get coverage into Sonar, apparently it is possible using SCCT [1] because it will output a cobertura file [2].


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Peter Önneby

unread,
Aug 29, 2013, 3:09:45 PM8/29/13
to play-fr...@googlegroups.com
Hey Lukasz

I've not tried since I last posted but I keep an eye on the jacoco4sbt plugin in case there are any developments.
I noticed you are too :). I'm guessing from reading your post that the latest version of jacoco4sbt still doesn't generate the coverage for Play apps?

Thanks,
Peter

Balázs Mária Németh

unread,
Oct 21, 2013, 10:19:58 AM10/21/13
to play-fr...@googlegroups.com
Hey, 

I almost managed to get everything work.

plugins.sbt:

resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"

libraryDependencies ++= Seq(
  "org.jacoco" % "org.jacoco.core" % "0.5.9.201207300726" artifacts(Artifact("org.jacoco.core", "jar", "jar")),
  "org.jacoco" % "org.jacoco.report" % "0.5.9.201207300726" artifacts(Artifact("org.jacoco.report", "jar", "jar"))
)

// Add Jacoco plugin library
addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.1")

qa.sbt in project root:

import sbt._
import Keys._
import play.Project._
import de.johoop.jacoco4sbt._
import de.johoop.jacoco4sbt.JacocoPlugin._

jacoco.settings

// Add the 'jacoco:cover' command to Play console. 
// Output file: target/jacoco/html/index.html
parallelExecution      in jacoco.Config := false

jacoco.outputDirectory in jacoco.Config := file("target/jacoco")

jacoco.reportFormats   in jacoco.Config := Seq(XMLReport("utf-8"), HTMLReport("utf-8"))

jacoco.excludes        in jacoco.Config := Seq("my.package.Global*", "*Global*", "*views*", "my.package*", "securesocial", "*Routes*", "*controllers*routes*", "*controllers*Reverse*", "*controllers*javascript*", "*controller*ref*")

Note that the "my.package.Global*" works only on my linux machine for me but not on win7, therefore I included "*Global*" as well.

Then my Sonar runner properties:

# required metadata
sonar.projectKey=myproject:key
sonar.projectName=myproject
sonar.projectVersion=1.0
 
# optional description
sonar.projectDescription=desc

#the sources of the application
sonar.sources=app
#the test sources
sonar.tests=test
#the compiled classes
sonar.binaries=target/scala-2.10/classes
 
# The value of the property must be the key of the language.
sonar.language=java
sonar.java.source=1.7
sonar.java.target=1.7
 
# Encoding of the source code
sonar.sourceEncoding=UTF-8

sonar.host.url=http://mydomain:9009
sonar.jdbc.url=jdbc:postgresql://mydomain/sonar
sonar.jdbc.username=sonar
sonar.jdbc.password=insertsomethingcreative

#tell sonar to reuse the reports
sonar.dynamicAnalysis=reuseReports

#where to find the junit report xmls
sonar.junit.reportsPath=target/test-reports

#code coverage plugin
sonar.java.coveragePlugin=jacoco

#jacoco exec path
sonar.jacoco.reportPath=target/jacoco/jacoco.exec

A remaining issue here is that play exports the junit reports in the format TestClassName.xml, whereas for some reason the report only gets picked up when prefixed with "TEST-".
The other issue is that while there's an elapsed time for the whole suite, there's always 0 (zero) inserted for the separate test cases.

So my question would be:
1, how to rename the report xml files?
2, is there a way to get the execution time for each test case?

thanks,
Balázs

Aurélien Allienne

unread,
Nov 4, 2013, 7:34:26 AM11/4/13
to play-fr...@googlegroups.com

Hi

I try to configure jacoco with your recommendation and I still have

Caused by: java.lang.NullPointerException
    at org.sonar.plugins.jacoco.ExecutionDataVisitor.visitClassExecution(ExecutionDataVisitor.java:48)

Can you take a look on my configuration :

        parallelExecution         in jacoco.Config := false,
        jacoco.outputDirectory    in jacoco.Config := file("target/jacoco"),
        jacoco.reportFormats      in jacoco.Config := Seq(XMLReport("utf-8"), HTMLReport("utf-8")),  
        jacoco.excludes           in jacoco.Config := Seq("views*", "*Routes*", "controllers*routes*", "controllers*Reverse*", "controllers*javascript*", "controller*ref*"),

    <!-- Jacoco configuration -->
    <property name="sonar.dynamicAnalysis" value="reuseReports" />
    <property name="sonar.junit.reportsPath" value="../target/test-reports" />
    <property name="sonar.java.coveragePlugin" value="jacoco" />    
    <property name="sonar.jacoco.reportPath" value="../target/jacoco/jacoco.exec" />
Did I miss something?

Regards

Roch Delsalle

unread,
Mar 27, 2014, 10:21:57 AM3/27/14
to play-fr...@googlegroups.com
A Sonar plugin has been released for scoverage : https://github.com/scoverage/sonar-scoverage-plugin

You can use it if you want to include statement level coverage / block level coverage in your Sonar dashboard.


--
Reply all
Reply to author
Forward
0 new messages