How to make Maven unit test code coverage work - java

In Eclipse, I have used EcLEmma to see the unit test code coverage. Which worked fine.
Therefore I have tried to use the JaCoCo plugin for Maven to see the same report with Surefire from Maven build, or even better, with a certain profile, or in the site cycle. Without success. All suggested solutions here didn't work for me.
What is the best way to get a unit test code coverage report (with surefire)?
[Edit]
to be more specific why jacoco failed for me.... as I got always the
Skipping JaCoCo execution due to missing execution data
from the pom
in the properties
<jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file>
<jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file>
in the Build section
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<versionRange>${jacoco.version}</versionRange>
<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>${jacoco.ut.execution.data.file}</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>${jacoco.ut.execution.data.file}</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
<!-- Prepares the property pointing to the JaCoCo runtime agent
which is passed as VM argument when Maven the Failsafe plugin is executed. -->
<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>${jacoco.it.execution.data.file}</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>${jacoco.it.execution.data.file}</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</pluginExecutionFilter>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
was my last try but the pom becomes bigger and bigger without any result
which failes with
configuring report plugin org.apache.maven.plugins:maven-jxr-plugin:2.3
configuring report plugin org.jacoco:jacoco-maven-plugin:0.7.5.201505241946
Skipping JaCoCo execution due to missing execution data file:......\target\jacoco.exec
Skipping JaCoCo execution due to missing execution data file:......\target\jacoco-it.exec
.... => long project path

Thanks user3732793
Personnaly, I only needed to add this to my pom.xml
<project>
...
<dependencies>
...
</dependencies>
<build>
<plugins>
...
<!-- Code Coverage report generation -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>generate-code-coverage-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Then I am running
mvn test
and I get the HTML report under ./target/site/jacoco/*.html

As always the solution is easy after reading the documentation which provides example poms jacoco documentation.
This profile:
<profile>
<id>test</id>
<properties>
<env>test</env>
<gebEnv>test</gebEnv>
<jacoco.skip>false</jacoco.skip>
<maven.test.skip>false</maven.test.skip>
<skip.unit.tests>false</skip.unit.tests>
</properties>
</profile>
This in the build section:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</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>
And this in the reporting section:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
</plugin>
Than this does all:
mvn clean install site -P test

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.

Jacoco-it.exec not getting generated for integration test

I have multi module project -
Parent
1- controller module - has unit tests
2- service module - has unit tests
3- integration test module - Rest assured Api testing
I have integrated jacoco in parent's pom.xml.
Jacoco.exec are getting generated for all the unit tests but jacoco-it.exec not getting generated for integration test module. However surefire-reporst are getting generated. Please suggest if I am missing anything? Do I need to add anything on integration-test-module's pom.xml ? All my plugins are in parent's pom.xml
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<executions>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.15</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<argLine>${failsafeArgLine}</argLine>
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
You need to use #{failsareArgLine} in the failsafe configuration argLine property, as cited in the documentation (so basically an # instead of $), otherwise failsafe will not use the property set by JaCoCo. In my project, at least, this was the only thing that was missing.

Exclude generated-sources from code coverage without impacting overall code-coverage

I have a project for which the code coverage numbers are pretty low. After doing a deep-dive I found out the jacoco code-coverage stats are depending on target directoy instead of src.
target/generated-sources/delombok/com/
If I exclude the target directory the coverage goes down 0%. So isnt code-coverage supposed to be measured against the code (src) rather than the target folder. Below is the screenshot from SonarQube.
So the question is how do i make sure the code coverage is measured for src instead of target?
Following is the pom without dependencies:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>removed</groupId>
<artifactId>xxxx</artifactId>
<version>0.0.11-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cassandra-maven-plugin</artifactId>
<version>2.1.7-1</version>
<configuration>
<script>${basedir}/src/main/cassandra/local_run.cql</script>
<cqlVersion>3.2.0</cqlVersion>
<startNativeTransport>true</startNativeTransport>
<nativeTransportPort>9142</nativeTransportPort>
<jmxPort>17199</jmxPort>
<storagePort>17000</storagePort>
<loadFailureIgnore>false</loadFailureIgnore>
<cuLoadFailureIgnore>false</cuLoadFailureIgnore>
</configuration>
<executions>
<execution>
<id>start</id>
<goals>
<goal>start</goal>
<goal>load</goal>
</goals>
<phase>process-classes</phase> <!--Bind cassandra start and load to process-classes which is the phase that is called up before tomcat is run -->
</execution>
<execution>
<id>stop</id>
<goals>
<goal>stop</goal>
</goals>
<!--Bind cassandra stop to generate-test-sources which is the phase that is called up before
tests are run. This is because our tests start their own cassandra -->
<phase>integration-test</phase>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
</plugin>
<!--Needed to add this so that src/main/java is recognized as a source folder -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<!-- Unit Test -->
<!-- Integration Test -->
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warName>activationloginedge</warName>
</configuration>
</plugin>
<!-- Tomcat Integration Test -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<path>/activationloginedge</path>
</configuration>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>run</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<systemProperties>
<!-- We want test configuration for running integration tests. -->
<archaius.deployment.environment>test</archaius.deployment.environment>
<logback-lib.env>filesystem</logback-lib.env>
</systemProperties>
<fork>true</fork>
</configuration>
</execution>
<execution>
<id>tomcat-shutdown</id>
<goals>
<goal>shutdown</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.4.201502262128</version>
<executions>
<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}/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}/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}/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.12.4</version>
<configuration>
<!-- Sets the VM argument line used when unit tests are run. -->
<argLine>${surefireArgLine}</argLine>
<!-- Skips unit tests if the value of skip.unit.tests property is true -->
<skipTests>${skip.unit.tests}</skipTests>
<!-- Excludes integration tests when unit tests are run. -->
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.15</version> <executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<!-- Sets the VM argument line used when integration tests are run-->
<argLine>${failsafeArgLine}</argLine>
<!-- Skips integration tests if the value of skip.integration.tests property is true-->
<skipTests>${skip.integration.tests}</skipTests> </configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
OK, the Lombok Maven plugin modifies the list of source folders during the Maven build, this is why the target/generated-sources/delombok folder is recognized by SonarQube as a source folder.
In order to force SonarQube to consider only your own source files, you should add the following property to your POM:
<properties>
<sonar.sources>src/main/java</sonar.sources>
</properties>
This will solve your problem.

Maven: execution from command line and multiple executions in config

I would like to execute a plugin goal from command line but perform multiple executions of the plugin. To this end my POM looks like this:
<plugin>
<groupId>xxx.yyy</groupId>
<artifactId>zzz</artifactId>
<version>1.1.6</version>
<executions>
<execution>
<id>default-cli-1</id>
<goals>
<goal>mygoal</goal>
</goals>
<configuration>
.... config1 ....
</configuration>
</execution>
<execution>
<id>default-cli-2</id>
<goals>
<goal>mygoal</goal>
</goals>
<configuration>
.... config2 ....
</configuration>
</execution>
</executions>
</plugin>
What I would like to do is something like:
mvn xxx.yyy.zzz:mygoal
and that would then execute the two executions. But I cannot figure out how.
I'm aware that I cannot use an <id> when executing from the command line. That is what the default-cli is for. However the <id> must be unique within <executions> which means I can only put the default-cli on one execution.
Maven version 3.0.5.
You can execute a goal (and its execution) from command line starting from Maven 3.3.1 on and this new feature, via the #executionId additional option.
Concerning Maven and execution ids generation, you can also check this SO question.
Before Maven 3.3.1 you could instead bind the two executions to a phase which would normally not harm (like validate) and have something like the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>execution-1</id>
<phase>validate</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>something1</classifier>
</configuration>
</execution>
<execution>
<id>execution-2</id>
<phase>validate</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>something2</classifier>
</configuration>
</execution>
</executions>
</plugin>
Then executing:
mvn validate
You will effectively execute the two executions of the same goal of the same plugin, as part of an harmless phase.
If you don't want to have them as part of this phase by default (understandable), then you can move them to a profile and activate it as part of the execution:
mvn validate -PpluginGoalExecution
For completeness, the profile would look like:
<profile>
<id>pluginExecution</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>execution1</id>
<phase>validate</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>something1</classifier>
</configuration>
</execution>
<execution>
<id>execution2</id>
<phase>validate</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>something2</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
And it goes without saying: the id of the profile should in this case be quite self explanatory about which plugin and which goal it would actually execute (that is, the purpose of the profile, as usual).
Update
Just cosmetic, but you could also add to the profiled build above the element:
<defaultGoal>validate</defaultGoal>
So that you would only need to run the following Maven command (only profile activation):
mvn -PpluginGoalExecution
And it would then automatically execute the validate phase and the configured plugin executions. Not a big change (as I said, cosmetic), but maybe closer to a plugin goal execution rather than a Maven phase invocation (again, just appearance).

Share Scala class in test folder with Java tests in Maven

I have a Maven project with mixed Java and Scala code. I want to use an auxiliary class located in the scala test folder for Java tests. The file tree is like below, omitting packages:
+ test/
+ java/...
- SomeTest.java
+ scala/...
- Aux.scala
- OtherTest.scala
I want to import code from Aux.scala for use in the SomeTest.java class. It works fine in my IDE, where all folders are marked as test folders. However when building this project in Maven I get an import error from the Java compiler.
How can I configure Maven to use the Scala test code for Java tests?
In order to resolve dependencies on Scala classes in the Java test-compile phase, you have to bind the testCompile goal of the scala-maven-plugin to the process-test-resources phase. That way, the Scala classes are already compiled when you compile the Java test classes.
The following snippet should do the trick:
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.4</version>
<executions>
<!-- Run scala compiler in the process-test-resources phase, so that dependencies on
scala classes can be resolved later in the (Java) test-compile phase -->
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
The complete build element of my pom.xml for a mixed Java/Scala project looks the following:
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.4</version>
<executions>
<!-- Run scala compiler in the process-test-resources phase, so that dependencies on
scala classes can be resolved later in the (Java) test-compile phase -->
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<stdout>W</stdout> <!-- Skip coloring output -->
</configuration>
<executions>
<execution>
<id>scala-test</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<suffixes>(?<!(IT|Integration))(Test|Suite|Case)</suffixes>
</configuration>
</execution>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<suffixes>(IT|Integration)(Test|Suite|Case)</suffixes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Categories

Resources