Jacoco glassfish jvm options

333 views
Skip to first unread message

mwfs...@gmail.com

unread,
Oct 3, 2017, 4:09:39 PM10/3/17
to JaCoCo and EclEmma Users
I have a Rest Webservice deployed on glassfish. The service has both unit & integration tests (tests which hit end points of the webserivce).The pom is in below format. When i run the verify goal , i am able to see the coverage report for unit tests, but the integration tests give 0% coverage. I was told by 1 of the jacoco developers that
"To get the coverage for integration tests you must make sure that this failsafeArgLine property is passed to JVM of application under test, i.e. JVM that executes glassfish, what is entirely and solely depends on a way you're launching it."

I am trying to start glassfish from intellij. There is a VM options where i need to put the failsafeArgLine. I am entering that in following way as of now.

-javaagent:c:\iat\mavenrepository\org\jacoco\org.jacoco.agent\0.7.9\org.jacoco.agent-0.7.9-runtime.jar=destfile=C:\dmahapat_JaxRsApp\target\jacoco-it.exec

This sets glassfish domain.xml jvm options
<jvm-options>-javaagent:c:/iat/mavenrepository/org/jacoco/org.jacoco.agent/0.7.9/org.jacoco.agent-0.7.9-runtime.jar=destfile=C:\dmahapat_JaxRsApp\target\jacoco-it.exec</jvm-options>

I am still getting 0% coverage. What am i doing wrong?

pom.xml structure

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.mathworks</groupId>
<artifactId>JaxRsApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>JaxRsApp</name>

<build>
<finalName>JaxRsApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-test-source</id>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}\src\integration-test\java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-resource</id>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}\src\integration-test\resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Surefire plugin is executed.
-->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<!--
Sets the name of the property containing the settings
for JaCoCo runtime agent.
-->
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!--
Ensures that the code coverage report for unit tests is created after
unit tests have been run.
-->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>

<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
<!--
Sets the name of the property containing the settings
for JaCoCo runtime agent.
-->
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<!--
Ensures that the code coverage report for integration tests after
integration tests have been run.
-->
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>

</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
<excludes>
<exclude>**/*IntegrationTest*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<argLine>${failsafeArgLine}</argLine>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
<excludes>
<exclude>**/*UnitTest.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.25.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>
</project>

Evgeny Mandrikov

unread,
Oct 3, 2017, 6:39:39 PM10/3/17
to JaCoCo and EclEmma Users
Your pom.xml uses

${project.build.directory}/coverage-reports/jacoco-it.exec

Your jvm-options use

destfile=C:\dmahapat_JaxRsApp\target\jacoco-it.exec

Is it really the same path???

mwfs...@gmail.com

unread,
Oct 4, 2017, 9:21:42 AM10/4/17
to JaCoCo and EclEmma Users
Hi Evgeny, thanks for catching that . I have changed the jvm options to
-javaagent:c:/iat/mavenrepository/org/jacoco/org.jacoco.agent/0.7.9/org.jacoco.agent-0.7.9-runtime.jar=destfile=C:/dmahapat_JaxRsApp/target/coverage-reports/jacoco-it.exec

So the glassfish domain.xml content looks like this now.
<jvm-options>-javaagent:c:/iat/mavenrepository/org/jacoco/org.jacoco.agent/0.7.9/org.jacoco.agent-0.7.9-runtime.jar=destfile=C:/dmahapat_JaxRsApp/target/coverage-reports/jacoco-it.exec</jvm-option>

This still gives a 0% coverage.

mwfs...@gmail.com

unread,
Oct 4, 2017, 9:52:58 AM10/4/17
to JaCoCo and EclEmma Users
Here is the warning that i am getting.
[INFO] --- jacoco-maven-plugin:0.7.9:report (post-integration-test) @ JaxRsApp ---
[INFO] Loading execution data file C:\dmahapat_JaxRsApp\target\coverage-reports\jacoco-it.exec
[INFO] Analyzed bundle 'JaxRsApp' with 6 classes
[WARNING] Classes in bundle 'JaxRsApp' do no match with execution data. For report generation the same class files must be used as at runtime.
[WARNING] Execution data for class org/mathworks/JaxRsApp/database/DatabaseClass does not match.
[WARNING] Execution data for class org/mathworks/JaxRsApp/model/Message does not match.
[WARNING] Execution data for class org/mathworks/JaxRsApp/service/MessageService does not match.
[WARNING] Execution data for class org/mathworks/JaxRsApp/resources/MessageResource does not match.

mwfs...@gmail.com

unread,
Oct 4, 2017, 10:22:07 AM10/4/17
to JaCoCo and EclEmma Users
Evgeny, finally i was able to make it work by changing jvm option to
-DargLine=-javaagent:c:/iat/mavenrepository/org/jacoco/org.jacoco.agent/0.7.9/org.jacoco.agent-0.7.9-runtime.jar=destfile=C:/dmahapat_JaxRsApp/target/coverage-reports/jacoco-it.exec

(added -DargLine=)

But the report shows 0% coverage sometimes & shows proper results some times. When it is showing 0%, i am restarting the server with -javaagent:c:/iat/mavenrepository/org/jacoco/org.jacoco.agent/0.7.9/org.jacoco.agent-0.7.9-runtime.jar=destfile=C:/dmahapat_JaxRsApp/target/coverage-reports/jacoco-it.exec JVM option & again restarting with the right jvm option (-DargLine=-javaagent:c:/iat/mavenrepository/org/jacoco/org.jacoco.agent/0.7.9/org.jacoco.agent-0.7.9-runtime.jar=destfile=C:/dmahapat_JaxRsApp/target/coverage-reports/jacoco-it.exec). Do you know why it might be showing 0% at times & works fine after server restart?

Evgeny Mandrikov

unread,
Oct 4, 2017, 11:48:01 AM10/4/17
to JaCoCo and EclEmma Users
first of all: exec file is written at the JVM termination ( as stated in documentation at http://www.jacoco.org/jacoco/trunk/doc/agent.html , please read documentation! ) by means of JVM shutdown hook ( see http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread) ) So it won't be written, if server is not terminated, or if killed instead of graceful termination.

secondly: classes on the server must match exactly (byte-to-byte) classes used during report generation and that's why you're seeing warnings ( btw please consider doing a search prior to asking question - http://lmgtfy.com/?q=Execution+data+does+not+match and again - this is described in documentation, please read it - http://www.jacoco.org/jacoco/trunk/doc/classids.html ) So if classes are modified, then re-deploy of classes / restart of server is needed.

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages