Ignore Classes and Methods form Code Coverage (jacoco) - java

In my application I want to ignore to classes (packageName1/Starter/packageName1/IO/CmdException.class) and for every class equals and hashCode method.
My pom.xml looks like:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.0.201403182114</version>
<configuration>
<instrumentation>
<ignores>
<ignore>*hashCode</ignore>
<ignore>*equals</ignore>
</ignores>
<excludes>
<exclude>packageName1/Starter.class</exclude>
<exclude>packageName1/IO/CmdException.class</exclude>
</excludes>
</instrumentation>
</configuration>
<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>
</plugins>
</build>
The problem is that coverage still threats these classes and methods.

This Works for me to exclude classes.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
<configuration>
<ignores>
<ignore>com.sample.package.bean.*</ignore>
</ignores>
<excludes>
<!-- all class which we want to exclude from the coverage report -->
<exclude>**/*class1.class</exclude>
<exclude>**/*class2.class</exclude>
<exclude>**/*class3.class</exclude>
<exclude>**/*class4.class</exclude>
</excludes>
</configuration>
<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>

Related

Exclude files/packages from SonarQube coverage

I have a package, com.org.projectname.model, in my project. What I want to do is exclude all the files within this package from SonarQube coverage. I tried,
<exclude>**/model /*.class</exclude> and <exclude>**/com/org/projectname/model/*.class</exclude>, but this didn't work.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<configuration>
<excludes>
</excludes>
</configuration>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>target/coverage-data/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/coverage-data/jacoco-ut.exec</dataFile>
<outputDirectory>target/coverage-reports/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
How to fix this issue? Or is there any other way?
Have you tried using sonar.exclusions in your POM properties?
<properties>
<sonar.exclusions>
**/model/*.java
</sonar.exclusions>
</properties>

Problem importing Java classes in groovy files Java 11

I've been trying to move an application to Java 11. This applications has multiple modules that use a common pom. After refactoring the main pom.xml to Java 11 I haven't been able to import Java classes correctly from Groovy classes and I keep getting this error from mvn clean compile "Error occurred while calling a method on a Groovy class from classpath.: InvocationTargetException: startup failed:". Here's my pom file configuration.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.7.0</version>
<executions>
<execution>
<goals>
<goal>execute</goal>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>compileTests</goal>
<goal>removeStubs</goal>
<goal>removeTestStubs</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
<formats>
<format>xml</format>
</formats>
</configuration>
<executions>
<execution>
<id>clean</id>
<phase>pre-site</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
<execution>
<id>instrument</id>
<phase>site</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<configuration>
<destFile>${project.basedir}/target/jacoco.exec</destFile>
<append>true</append>
</configuration>
<executions>
<execution>
<id>agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/groovy</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/groovy</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
What am I doing wrong?

Jacoco coverage percentage maven

When runnning mvn test, my spring boot application builds succesfully. When I do mvn clean install or mvn clean verify, build fails. This is because I have minimum code coverage ratio configured to 80%. When I run mvn clean install/verify, the result shows instructions covered ratio is 0.00, but expected minimum is 0.50.
Before setting the minimum threshold, running mvn clean install, gave the below result, which says 65% covered. What is missing in my configuration? Am I missing anything? How can I get jacoco pick the correct code covered?
Here is my pom
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>0.8.5</version>
<classifier>runtime</classifier>
<scope>test</scope>
</dependency>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
<dependencies>
<dependency>
<groupId>com.github.bitmc</groupId>
<artifactId>checkstyle</artifactId>
<version>8.35.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<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>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<excludes>
<exclude>com.pip</exclude>
</excludes>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.50</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
I am using java version "1.8.0_202"
In you's plugin config need set like that;
`
...
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<configuration>
<excludes>
<exclude>**/PACKEGE YOU WANT EXCLUDE.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>65%</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
`
in your's exemple the tag pluguin is out from plugins tag.
If you are using Jacoco with PowerMockito, and the class which you want to cover is taken in PrepareForTest, then in that case it will show you 0% coverage.
Try removing the target class from PrepareForTest

Is it possible to only run the JaCoCo maven plugin during the site lifecycle?

Consider the follow build configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${surefireArgLine}</argLine>
<parallel>all</parallel>
<threadCount>2</threadCount>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>${failsafeArgLine}</argLine>
<parallel>classes</parallel>
<threadCount>2</threadCount>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
</configuration>
</execution>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/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}/jacoco-it.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
And reporting configuration:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<reportSets>
<reportSet>
<id>JaCoCo-UnitTest</id>
<reports>
<report>report</report>
</reports>
<configuration>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
</configuration>
</reportSet>
<reportSet>
<id>JaCoCo-IntegrationTest</id>
<reports>
<report>report</report>
</reports>
<configuration>
<dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
</configuration>
</reportSet>
</reportSets>
</plugin>
The awesome part is mvn clean site works awesome, I get my reports. However, this now means that the JaCoCo agent is starting during the normal lifecycle, ie: mvn clean install verify.
Is there a way to disable the JaCoCo agent during the normal lifecycle but not the site cycle?
Simply skip the JaCoCo execution using a property:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Skip this phase if jacoco.skip property is set to true -->
<skip>${jacoco.skip}</skip>
<destFile>${project.build.directory}/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Skip this phase if jacoco.skip property is set to true -->
<skip>${jacoco.skip}</skip>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
You can also disable JaCoCo execution when tests are skipped:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
<!-- Skip tests if skip.unit.tests property is set to true -->
<skipTests>${skip.unit.tests}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Skip this phase if unit tests are skipped -->
<skip>${skip.unit.tests}</skip>
<destFile>${project.build.directory}/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Skip this phase if unit tests are skipped -->
<skip>${skip.unit.tests}</skip>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>

Is there any maven sleep functionality?

I have maven profile set for testing where in pre-integration-tests maven starts two jetty servers and afterwards tests are started. The problem I've stumbled into is in the servers, they aren't fully loaded when tests start. It seems like the problem is fixed by adding 5 second sleep time into the tests, but I wish to add it in maven and remove from tests. Is there anyway I could do that? Please look into code sample below for additional information
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy</id>
<phase>compile</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.work.projectname</groupId>
<artifactId>projectname-web</artifactId>
<version>${project.version}</version>
<type>war</type>
<destFileName>projectname-${project.version}.war</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.work.projectname</groupId>
<artifactId>projectname-stubs-web</artifactId>
<version>${project.version}</version>
<type>war</type>
<destFileName>projectname-stubs-${project.version}.war</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-server.version}</version>
<executions>
<execution>
<id>start-projectname</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy-war</goal>
</goals>
<configuration>
<daemon>true</daemon>
<reload>manual</reload>
<war>${project.build.directory}/projectname-${project.version}.war</war>
<webAppXml>${basedir}/src/jetty/jetty-loginService.xml</webAppXml>
<webAppConfig>
<contextPath>/projectname</contextPath>
<parentLoaderPriority>true</parentLoaderPriority>
<tempDirectory>${project.build.directory}/tmp-projectname</tempDirectory>
</webAppConfig>
<stopPort>8006</stopPort>
<stopKey>STOP</stopKey>
</configuration>
</execution>
<execution>
<id>start-projectname-stubs</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy-war</goal>
</goals>
<configuration>
<daemon>true</daemon>
<reload>manual</reload>
<war>${project.build.directory}/projectname-stubs-${project.version}.war</war>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>${stub-jetty-server.port}</port>
<maxIdleTime>300000</maxIdleTime>
</connector>
</connectors>
<stopPort>8008</stopPort>
<stopKey>STOP</stopKey>
</configuration>
</execution>
<execution>
<id>stop-projectname</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<stopPort>8006</stopPort>
<stopKey>STOP</stopKey>
</configuration>
</execution>
<execution>
<id>stop-projectname-stubs</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<stopPort>8008</stopPort>
<stopKey>STOP</stopKey>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<configuration>
<parallel>classes</parallel>
<threadCountClasses>33</threadCountClasses>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You can do it with the help of the maven antrun plugin. Something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<configuration>
<tasks>
<sleep seconds="5" />
</tasks>
</configuration>
<executions>
<execution>
<id>sleep-for-a-while</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
EDIT: In case someone will catch this. Based on the comment from jl, tasks have indeed been deprecated and here is the same thing based on using targets instead. Sligthly reorganized but has the same function.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>sleep-for-a-while</id>
<phase>pre-integration-test</phase>
<configuration>
<target>
<sleep seconds="5" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I have created a maven-sleep-plugin once. Not sure how much it works now, you can try and let us know.
Usage (after checking out the source and building):
<plugin>
<groupId>org.jboss.maven.plugins</groupId>
<artifactId>maven-sleep-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>sleep-5-seconds</id>
<phase>pre-integration-test</phase>
<goals>
<goal>sleep</goal>
</goals>
<configuration>
<delay>5</delay>
</configuration>
</execution>
</executions>
</plugin>

Categories

Resources