QA Automation : API Code Coverage with JaCoCo and Sonarqube .

  1. Jacoco and Sonarqube jars.
  2. Jenkins File
  3. Docker file
  4. Create Sonarqube server — will discuss the steps .
  5. Note —You should have Docker installed on your machine , Jenkins server for CI/CD and repo’s on github.
  • Application Under Test
  • Automation Repo
  1. Jenkins and Sonarqube jars —

Jacoco-cli-nodeps.jar

Org.jacoco.agent-0.8.5-runtime.jar

Sonar-scanner-cli-4.6.0.2311.jar

#!groovy

node(‘<<your node slave>>’){
try {
stage(“Cleanup Workspace”){ deleteDir() }
stage(“Checkout”){ checkout scm }
stage(“Build api-service”){ service() }
stage(“Run automation”){ testExecution()}
stage(“Report”){ report()}

}catch (err) {

throw err
}
}

def service(){

dir(‘api-service’) {


sh ‘cp /var/jenkins/workspace/QA_Micro-Services-Coverage/target/<<product-api>>.jar <<product-api>>.jar’
}

}

testExecution() — This method will be responsible to deploy jar on container and execute the tests and prepare coverage xml report

def testExecution() {

script {
serviceImage = docker.build ‘api-service:snapshot’
//container = serviceImage.run(‘-p 8080:8080’)
//docker.image(serviceImage).withRun { c ->
serviceImage.withRun(‘-p 8080:8080 -p 6300:6300’) { c ->
//input message: “continue?”
sh ‘netstat -tulpa’
withMaven(maven: ‘Maven_3_3_9’, jdk: ‘Java_Jenkins_JDK8u221’){
sh “mvn clean install package -Dsuite=testng_suite.xml”
}

sh ‘’’

java -jar coverage/jacoco-cli-nodeps.jar dump — destfile target/automation.exec
java -jar coverage/jacoco-cli-nodeps.jar report target/automation.exec — classfiles /var/jenkins/workspace/<<QA Job>>/target/classes — xml target/automation.xml
‘’’
}
}

}
Report () — responsible to publish a report on Sonar.

def report(){

sh ‘’’
java -Dsonar.host.url=http://<<sonar_host>>:9000 -Dsonar.projectKey=service-api -Dsonar.login=<<login>> -Dsonar.password=<<pwd>> -Dsonar.projectBaseDir=/var/jenkins/workspace/<<Jenkins job name-application>> -Dsonar.java.binaries=target -Dsonar.coverage.jacoco.xmlReportPaths=../target/automation.xml -Dsonar.java.libraries=/var/jenkins/workspace/<<Jenkins job name-automation>>/coverage/jacoco-cli-nodeps.jar -jar /var/jenkins/workspace/<Jenkins job name-automation>>/coverage/sonar-scanner-cli-4.6.0.2311.jar
‘’’
}

FROM openjdk:9-jre

COPY api-service/<<application>>.jar /opt/app/<<application>>.jar
COPY coverage/org.jacoco.agent-0.8.5-runtime.jar /opt/app/agent.jar
WORKDIR /opt/app
RUN sh -c ‘touch /opt/app/<<application>>.jar’

EXPOSE 8080
EXPOSE 6300

ENTRYPOINT [“java”, “-javaagent:/opt/app/agent.jar=output=tcpserver,address=0.0.0.0,port=6300”,”-DAPP_HOME=/opt/app/”,”-jar”, “/opt/app/<<application>>.jar”]

  • Fire the below command to run Sonarqube on Docker
  • docker run -d — name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9001:9000 sonarqube:latest
  • Navigate to below link
  • http://localhost:9000/
  • setup your userName and Password
  • Create Project — provide the project and click on setup
  • Generate Token — Enter the token name which will generate the token which you can use it in your pom.xml file (if you are going to use jacoco maven plugin then you can use generated token — this is out of scope for this article)
  • Create two Freestyle Jenkins job — 1. AUT and 2. Automation project
  • Application Under Test :

Provide your jenkins slave details under General/Restrict Where This Project Can be Run

Click on Git radio button and enter repository details , do not forget to mention your branch

  • Automation Project :

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store