/scala-sonar

Primary LanguageHTMLCreative Commons Zero v1.0 UniversalCC0-1.0

System (E2E) tests of Scala application with Jacoco and Sonar test coverage

Introduction

Simple Scala application taken from Play Framework Scala examples: https://github.com/playframework/play-samples/tree/2.7.x/play-scala-rest-api-example
In order to get code coverage of running application, we use Jacoco agent.
Sonar can parse report generated by Jacoco agent.

Setup

  • Have jacocoagent.jar file (it is in project directory in our case)
  • Have pom.xml file to run Jacoco tasks (also in project directory)
  • Have local Sonar instance running (e.g. on port 9001)
  • Create Sonar project (scala-sonar in our case)
  • sonar-scanner should be added to PATH.
  • Set sbt options:
export SBT_OPTS="-javaagent:jacocoagent.jar=destfile=jacoco-it.exec,output=file,append=true,dumponexit=true"

Run AUT and perform tests

Run Scala application:

sbt run

Application is up and running on localhost:9000

At this point, we can run system e2e tests. Let's emulate some test case:

GET http://localhost:9000/v1/posts

Get test coverage results

In order to get Jacoco report, we should stop application under test.
Generate Jacoco report with maven:

mvn antrun:run@generate-report -Dskip.int.tests.report=false

In order to export Jacoco report to Sonar, we should know path to Jacoco coverage result xml file. It is target/coverage-report/coverage-report.xml.

sonar-scanner -Dsonar.projectKey=scala-sonar -Dsonar.sources=app -Dsonar.host.url=http://localhost:9001 -Dsonar.login=5ba67cce94830ebec270975bb32ede25868e0cc1 -Dsonar.coverage.jacoco.xmlReportPaths=target/coverage-report/coverage-report.xml

Sonar report is available at http://localhost:9001/dashboard?id=scala-sonar