Code coverage of client/server web application - java

I am writing a multi-module application. Some of the modules are just basic Java libraries which are then included in the WAR of a webapp.
I would like to run code coverage in the following scenario:
I am running the webapp through an embedded Jetty that is started via Maven.
I have tests which are executing HTTP requests against the webapp.
I would like to get code covered in the webapp and also by the tests.
Is this possible and how can it be achieved with Cobertura, JaCoCo or Emma? From what I understand, the code coverage will only cover the client-side code in this scenario. Am I correct?

I think if you would manage to attach the JaCoCo-agent to the jvm that runs the jetty, it should be able to measure which code has been called over the time you run the integration tests against your webapp. So you should get a statistic that shows you the code coverage.
There is a JaCoCo Maven Plugin - though I'm not sure if this will help with you scenario. Just used it during unit tests.
Edit: found a blog-post that seems to point in the right direction here
Measure Code Coverage by Integration Tests with Sonar

Here's how I achieved it
Assuming you already have a minimal pom.xml config:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</
<version>0.7.4.201502262128</vers
</plugin>
Download JaCoCo's agent and copy jacocoagent.jar to a suitable location (e.g. $HOME/tools/jacocoagent0.7.4.jar)
Attach JaCoCo's agent to Maven's JVM via:
export MAVEN_OPTS="$MAVEN_OPTS \
-javaagent:$HOME/tools/jacocoagent0.7.4.jar=output=tcpserver,port=6300"
Run your application with embedded jetty server e.g. mvn jetty:run
Run your integration tests
In another shell, dump and report via mvn jacoco:dump jacoco:report
Open your report on ./target/site/index.html (by default)

You can use Jacoco plugin to generate code coverage Here is the plugin configuration I used for junit test code coverage.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.5.10.201208310627</version>
<configuration>
<skip>${maven.test.skip}</skip>
<output>file</output>
<append>true</append>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Note: you may get life cycle not covered error in maven while using eclipse, one way is you explicitly mention the life cycle using plugin management. I installed the jacoco plugin from the market place which resolved my problem

We had a similar scenario where integration test were run on a jetty server. Also we needed a combined report for all the tests unit and integration. The solution we implemented was to run-forked jetty and pass the jvmargs with the jacoco javaagent details. Our code coverage reports covered all the rest api's and the service layer java code.
The pom config for jacoco
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<configuration>
<append>true</append>
</configuration>
<executions>
<execution>
<id>prepare-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>prepare-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco.exec</destFile>
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
</executions>
</plugin>
With the above config we generated a common exec file for both unit and integration test. Next we configured jetty to run-forked
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-maven-plugin.version}</version>
<configuration>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
<webApp>
<contextPath>/myway</contextPath>
<descriptor>src/main/webapp/WEB-INF/web.xml</descriptor>
</webApp>
<!-- passing the jacoco plugin as a jvmarg -->
<jvmArgs>${failsafeArgLine}</jvmArgs>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<configuration>
<daemon>true</daemon>
<waitForChild>false</waitForChild>
</configuration>
<goals>
<goal>run-forked</goal>
</goals>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
This would launch jetty in a separate jvm with the jvmargs. Finally we generated the report in the reporting tag of the pom. We noticed that adding the report to the build plugins did not capture the integration tests run by the jetty.
<reporting>
</plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<reportSets>
<reportSet>
<id>jacoco-report</id>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
The reports can be accessed at target/site/jacoco/index.html, alternately you can run it from the command line.
mvn jacoco:report
Hope it helps.

Related

Not able to generate coverage report using jacoco plugin for test cases written using jmockit library

I have unit test cases written using jmockit(version:1.44) library in my application. I am able to run the test cases using maven surefire plugin and generate coverage reports using below dependency-
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-javaagent:${project.basedir}/repo/org/jmockit/1.44/jmockit-1.44.jar
</argLine>
<forkMode>once</forkMode>
</configuration>
</plugin>
However, I want to integrate coverage report with sonar qube and started using jacoco plugin instead of surefire.
But I am getting the exception -
[ERROR] PwdResetControllerTest.setUp:76 NoClassDefFound mockit.MockUp
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Please help.

Running single integration test inside docker in maven

I am trying to run integration testing on my maven project inside a docker container. I am using dockerfile-maven plugin to move jar file inside my docker container and maven-failsafe-plugin to initiate my integration testing. But somehow integration testing starts before docker being built. Also it doesn't call my test functions.
Here is my pom.xml snippet.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<includes>
<include>**/*.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.7</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>rohitbarnwal7/presto_him</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>plugin-${project.version}-jar-with-dependencies.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
Update: With above configuration, integration test class are being called but none of the test is actually executed.
Any help will be highly appreciated. Thanks.
You need to bind the building of the image to a maven phase that runs before integration test.
As documented in Bind Docker commands to Maven phases, you can bind building the docker image to a specific maven phase as such:
build-image
compile
package
In this case, building the image will take place at the maven package phase which will run before integration-test phase

Jacoco SonarQube Integration

I've spent a good couple of days trying to get SonarQube to display unit test code coverage from the Maven Jacoco plugin.
The error message I am stuck on is
[INFO] Analysing .../target/jacoco.exec
[WARNING] Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?
The report under target/site/jacoco/index.html generates as expected and contains line highlighting and line numbers. I have read that if no debug information is included in the compiled classes then the highlighting and line numbers will not show in this report.
I have read Maven includes debug information by default, however, just in case I included the following configuration in my projects maven-compiler-plugin setup
<configuration>
<debug>true</debug>
<debuglevel>lines,vars,source</debuglevel>
</configuration>
I have the following properties in my settings.xml (host and login left out on purpose)
<properties>
<sonar.host.url></sonar.host.url>
<sonar.login></sonar.login>
<sonar.ws.timeout>300</sonar.ws.timeout>
</properties>
I have the following configuration of my Jacoco plugin.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
My jacoco.exec is in the default location of target/jacoco.exec and is found correctly by SonarQube.
Relevant versions
Maven 3.2.2
Maven Compiler 3.5.1
Maven Surefire 2.19.1
Java 1.8.0_11
SonarQube Server 5.6
Jacoco Maven Plugin 0.7.7.201606060606
Mac OS X 10.10.5
Thanks in advance for the help!
---- EDIT ----
I am running the following maven commands
mvn clean package
mvn sonar:sonar
I have followed the article here and it just work for me, I have only updated the code to reflect the latest JaCoCo plugin version
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
</dependency>
and adding JaCoCo agent to my POM plugins:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
<!-- JaCoCo configuration -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
and running the following maven command (in Jenkins):
clean deploy $SONAR_MAVEN_GOAL -Dsonar.host.url=$SONAR_HOST_URL
I have SonarQube 6.3.0.19869, Jenkins 2.46.1, Maven Integration Plugin in Jenkins 2.15.1 (the older one caused JVM issues in my stack).

custom java action in maven phase

i am using javemelody to monitor performance of my app. i am using jetty maven plugin which starts up during mvn install and runs all test cases before generating the war. i wanted to generate a pdf report at the end of post-integration-test phase.
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
i was thinking if i can access the report url of embedded jetty to access javamelody, then maybe i can download my report to some location like using
curl localserver/context/monitoring?reports=pdf
is it possible to execute a custom script/java program in the post-integration-test phase before shutting down the embedded jetty ?
I would suggest to start jetty in the pre-integration-test phase do what ever you need to do in the integration-test phase and shutdown in post-integration phase.
Everything you need to do can be run via an integration test by the usage of the maven-failsafe-plugin.
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
with the above setup you can simply write an integration test for example WhatEverINeedToDoIT.java in src/test/java location and run it.

Glassfish container via Cargo isn't stopped by maven when integration tests fail

I've setup Cargo to start an instance of glassfish during the pre-integration-test phase in a maven profile. My tests are then run in the integration-test phase and, finally, cargo shuts down the tomcat instance in the post-integration-test phases.
This works great when all tests pass, but if any test fails, the maven build fails, and it appears that the post-integration-test phase is never reached, which leaves the glassfish instance running (and me unable to stop it without killing the process).
Am I doing something wrong? Is there a way to make sure cargo shuts down my glassfish instance, even if the integration-test phase fails?
My maven profile:
<profile>
<!-- run integration tests against the app deployed to a container -->
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- override the exclusion and include integration tests -->
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>***IntegrationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>${cargo.plugin.version}</version>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<container>
<containerId>glassfish3x</containerId>
<artifactInstaller>
<groupId>org.glassfish.main.distributions</groupId>
<artifactId>glassfish</artifactId>
<version>${glassfish.version}</version>
</artifactInstaller>
</container>
<configuration>
<properties>
<cargo.datasource.datasource.mysql>
cargo.datasource.jndi=jdbc/TrackerPool|
cargo.datasource.driver=com.mysql.jdbc.Driver|
cargo.datasource.url=jdbc:mysql://localhost/[database]|
cargo.datasource.transactionsupport=LOCAL_TRANSACTION|
cargo.datasource.username=[username]|
cargo.datasource.password=[password]
</cargo.datasource.datasource.mysql>
</properties>
</configuration>
</configuration>
</plugin>
</plugins>
</build>
</profile>
The problem is simply based in the wrong usage of the maven-surefire-plugin which is intended for using in relation with unit test but not for integration tests. For such purposes the maven-failsave-plugin exist which will solve your problem.
The usage of the maven-failsave-plugin release you from defining of include rule for integration tests. The usual naming convention in Maven for integration tests is like this:
IT*.java
*IT.java
*ITCase.java
So i would suggest to name your integration tests accordingly so you don't need any kind of exlude/include rule neither for maven-surefire-plugin (unit tests) nor for maven-failsafe-plugin (integration tests).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.13</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
The verify goal is only needed if you like to fail your build in case of failing integration tests. You have to call maven like this:
mvn -Pprofile clean verify

Categories

Resources