Saturday, May 16, 2020

Liferay Aquillian Extension : Jacoco Report - Code Coverage Report

Hi,

In this post I am going to discuss about taking code coverage report for the integration and functional test the we have seen in the posts mentioned below...

1. Liferay Arquillian Extension : Integration Testing 
2. Liferay Arquillian Extension : Functional Testing 

Prerequisites
Name Version Download Page Direct Link
Java 1.8 Java Download Page N/A
Mysql 8.0.20 N/A N/A
Liferay Portal 7.3.0 - CE Lifeay Portal Download Page Click to Download Directly
IntelliJ IDE 2019.3.5 N/A N/A

My request to you is to go through the posts which talk about Integration and Functional test prior to this.

Those two posts mentioned above talks the configuration details or the fundamentals for having Code Coverage Report using Jacoco (Java Code Coverage)

Lets talk business...

To incorporate the code coverage report for the liferay plugins that we develop.... we must update the build.gradle file associated with the plugin...

Updating build.gradle of the plugin/bundle
Open the build.gradle file associated with the plugin hello-world and modify the file accordingly, the different sections involved would be given below for you to refer...

1. Java Imports
import javax.management.remote.JMXConnectorFactory
import javax.management.remote.JMXServiceURL

2. Adding Jacoco Plugin
apply plugin: 'jacoco'

jacoco {
toolVersion = '0.7.9'
}

3. Creating copyJacocoAgent task
task copyJacocoAgent(type: Copy) {
println configurations.jacocoAgent

configurations.jacocoAgent.asFileTree.each {
from(zipTree(it))
}

into "${rootDir}/build/jacoco"
}

4. Creating dumpJacoco task
task dumpJacoco {
doLast {
def serverUrl = 'service:jmx:rmi:///jndi/rmi://localhost:8099/jmxrmi'
String beanName = "org.jacoco:type=Runtime"
        def jmxServiceUrl = new JMXServiceURL(serverUrl)
def server = JMXConnectorFactory.connect(new JMXConnectorFactory(jmxServiceUrl)).MBeanServerConnection
def gmxb = new GroovyMBean(server, beanName)

println "Connected to:\n$gmxb\n"
println "Executing dump()"
gmxb.dump(true)
}
}

5. Creating jacocoTestReport closure
jacocoTestReport {
dependsOn dumpJacoco
group = "Reporting"
reports {
xml.enabled true
csv.enabled false
//html.destination "${buildDir}/reports/coverage"
}
executionData = files("${rootDir}/build/jacoco/testIntegration.exec")
}

Updating setenv.sh of Tomcat Container
Open up setenv.sh file of the tomcat which you can find under bundles/tomcat-9.0.17/bin and modify the file with the following conent...

JACOCO_OPTS="-javaagent:/home/ravaneswaran/projects/liferay-arquillian-example/bundles/tomcat-9.0.17/bin/jacocoagent.jar"
CATALINA_OPTS="${CATALINA_OPTS} ${JACOCO_OPTS}"

the path of the jacocoagent.jar is very important which will be referred by the testIntegration task of the gradle to prepare the code coverage report...

After the update the setenv.sh file should appear something like the one given in the image below...



Copying the jacocoagent.jar file to tomcat bin directory
You can download the jar online or else you can perform a search in IntelliJ installed directory to get the jar file...

A sample search is given below for your reference...



Now copy and paste the file in tomcat's bin directory...

It is not mandatory to have the file in tomcat's bin directory but the path where you have the jacocoagent.jar should be mentioned in setenv.sh

With this we have completed the configuration to have jacoco code coverage report...

Running testIntegration command
Now run the testIntegration command to have the tests executed...

The result of the execution would be like the one shown below...



You can find the generated report under the folder build/reports/test/testIntegration of the plugin.

Refer the image below for details....



Open index.html file in your favourite browser to see the test summary...


Well this concludes the post and hope you had great time learning.

Would like to hear from you about this post, please do comment.

Thanks.

No comments:

Post a Comment

How to change the root password in linux when it is forgotten/to change

This blog is all about changing the root password of the Linux system when it is forgotten or to reset the password...   Let's get it ...