I have setup Jacoco in my project to do a) check for the code coverage and b) produce a coverage report. The generated HTML report shows the right coverage values but the warning messages that Jacoco check generates at the end of the build process shows different and wrong values. I wonder what I'm doing wrong here.
For example here is what I can see for a util class in the generated HTML report:
but for the same class I see this when the build is finished:
Here is my jacoco-report config in my pom.xml file:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>jacoco.check</id>
<goals>
<goal>check</goal>
</goals>
<phase>${jacoco.check.phase}</phase>
<configuration>
<skip>${jacoco.skip}</skip>
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>CLASS</element>
<excludes>
<exclude>com/xxxxxx/**/config/**/*</exclude>
<exclude>com/xxxxxx/**/filter/**/*</exclude>
<exclude>com/xxxxxx/xxxxxx/errors/*</exclude>
<exclude>com/xxxxxx/xxxxxx/security/*</exclude>
<exclude>com/xxxxxx/xxxxxx/xxxxxxOnlineWebApplication.class</exclude>
</excludes>
<limits>
<limit implementation="org.jacoco.report.check.Limit">
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.90</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
<execution>
<id>prepare-unit-tests</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<propertyName>itCoverageAgent</propertyName>
</configuration>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
and my jacoco-check config is as follows:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>agent-for-ut</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<append>true</append>
<destFile>${sonar.jacoco.reportPaths}</destFile>
</configuration>
</execution>
<!-- Integration-tests executed with failsafe-plugin -->
<execution>
<id>agent-for-it</id>
<phase>package</phase>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
<configuration>
<append>true</append>
<destFile>${sonar.jacoco.itReportPath}</destFile>
</configuration>
</execution>
</executions>
<configuration>
<excludes>
<exclude>com/xxxxxx/**/config/**/*</exclude>
<exclude>com/xxxxxx/**/filter/**/*</exclude>
<exclude>com/xxxxxx/xxxxxx/errors/*</exclude>
<exclude>com/xxxxxx/xxxxxx/security/*</exclude>
<exclude>com/xxxxxx/xxxxxx/xxxxxxOnlineWebApplication.class</exclude>
</excludes>
</configuration>
</plugin>
It looks like you are changing the location that the results are stored in:
<destFile>${sonar.jacoco.itReportPath}</destFile>
You will also need to tell the check plugin where to find the data file using the <dataFile> item to your rules configuration.
Please see the jacoco:check docs for more information.
Related
I'm trying to test my simple API in wso2 using the Redis connector. The GET request is sent to the API bank via its endpoint to retrieve exchange rates and parsing the data into the Redis cache.
I wrote a unit test for the API and mockservice according to the documentation https://ei.docs.wso2.com/en/7.2.0/micro-integrator/develop/creating-unit-test-suite/.
But when I tried to run this test, I got the following error :
[error] Artifact data reading failed
java.nio.file.NoSuchFileException: ..\NBU_EXCHANGE_DATA_INDEX_660_EI\NBU_EXCHANGE_DATA_INDEX_660_EIConnector\redis-connector-1.0.1.zip
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.Files.readAllBytes(Files.java:3152)
at org.wso2.synapse.unittest.SynapseTestCaseFileReader.processConnectorResourcesData(SynapseTestCaseFileReader.java:290)
at org.wso2.synapse.unittest.SynapseTestCaseFileReader.processArtifactData(SynapseTestCaseFileReader.java:91)
at org.wso2.synapse.unittest.UnitTestClient.executeTests(UnitTestClient.java:54)
at org.wso2.synapse.unittest.UnitTestCasesMojo.testCaseRunner(UnitTestCasesMojo.java:141)
at org.wso2.synapse.unittest.UnitTestCasesMojo.execute(UnitTestCasesMojo.java:79)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
[error] Error while creating a deployable message with test suites`
This my unit-test.xml :
<unit-test>
<artifacts>
<test-artifact>
<artifact>/NBU_EXCHANGE_DATA_INDEX_660_EIConfigs/src/main/synapse-config/api/WRITE_EXCHANGE_TO_REDIS.xml</artifact>
</test-artifact>
<supportive-artifacts>
<artifact>/NBU_EXCHANGE_DATA_INDEX_660_EI/NBU_EXCHANGE_DATA_INDEX_660_EIConfigs/src/main/synapse-config/sequences/WRITE_INDEX_EXCHANGE_TO_REDIS_XML.xml</artifact>
<artifact>/NBU_EXCHANGE_DATA_INDEX_660_EI/NBU_EXCHANGE_DATA_INDEX_660_EIConfigs/src/main/synapse-config/endpoints/EXCHANGE_XML_FROM_SITE_NBU_EP.xml</artifact>
</supportive-artifacts>
<registry-resources/>
<connector-resources>
<connector-resource>/NBU_EXCHANGE_DATA_INDEX_660_EI/NBU_EXCHANGE_DATA_INDEX_660_EIConnector/redis-connector-1.0.1.zip</connector-resource>
</connector-resources>
</artifacts>
<test-cases>
<test-case name="GetXML_exchange">
<input>
<request-path>/nbu-exchange/xml</request-path>
<request-method>GET</request-method>
<request-protocol>http</request-protocol>
</input>
<assertions>
<assertEquals>
<actual>$body</actual>
<expected><![CDATA[{
"Date": "2022-09-14",
"HTTP_SC": "200",
"XML_connect": "true",
"MAIN_XML_isUpdate": "false",
"TMP_XML_isUpdate": "false",
"NBU_XML_isUpdate": "false"
}]]></expected>
<message>not equals</message>
</assertEquals>
<assertNotNull>
<actual>$body</actual>
<message>not null</message>
</assertNotNull>
</assertions>
</test-case>
</test-cases>
<mock-services>
<mock-service>/NBU_EXCHANGE_DATA_INDEX_660_EI/NBU_EXCHANGE_DATA_INDEX_660_EIConfigs/test/resources/mock-services/ImitationNBU_EP_XML.xml</mock-service>
</mock-services>
</unit-test>
This my mock-service.xml :
<mock-service>
<service-name>EXCHANGE_XML_FROM_SITE_NBU_EP</service-name>
<port>9090</port>
<context>/nbu-exchange</context>
<resources>
<resource>
<sub-context>/xml</sub-context>
<method>GET</method>
<response>
<status-code>200</status-code>
<payload><![CDATA[<?xml version="1.0" encoding="utf-8"?>
<exchange>
<currency>
<r030>36</r030>
<txt>Австралійський долар</txt>
<rate>25.2652</rate>
<cc>AUD</cc>
<exchangedate>14.09.2022</exchangedate>
</currency>
<currency>
<r030>124</r030>
<txt>Канадський долар</txt>
<rate>28.1969</rate>
<cc>CAD</cc>
<exchangedate>14.09.2022</exchangedate>
</currency>
</exchange>]]></payload>
<headers>
<header name="Content-Type" value="application/xml"/>
</headers>
</response>
</resource>
</resources>
</mock-service>
This my pom.xml for test
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>NBU_EXCHANGE_DATA_INDEX_660_EI</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>NBU_EXCHANGE_DATA_INDEX_660_EIConfigs</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>NBU_EXCHANGE_DATA_INDEX_660_EIConfigs</name>
<description>NBU_EXCHANGE_DATA_INDEX_660_EIConfigs</description>
<properties>
<CApp.type>bpel/workflow=zip,lib/registry/filter=jar,webapp/jaxws=war,lib/library/bundle=jar,service/dataservice=dbs,synapse/local-entry=xml,synapse/proxy-service=xml,carbon/application=car,registry/resource=zip,lib/dataservice/validator=jar,synapse/endpoint=xml,web/application=war,lib/carbon/ui=jar,service/axis2=aar,synapse/sequence=xml,synapse/configuration=xml,wso2/gadget=dar,lib/registry/handlers=jar,lib/synapse/mediator=jar,synapse/task=xml,synapse/api=xml,synapse/template=xml,synapse/message-store=xml,synapse/message-processors=xml,synapse/inbound-endpoint=xml,synapse/metadata=yaml</CApp.type>
<maven.test.skip>false</maven.test.skip>
</properties>
<repositories>
<repository>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<id>wso2-nexus</id>
<url>https://maven.wso2.org/nexus/content/groups/wso2-public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<id>wso2-nexus</id>
<url>https://maven.wso2.org/nexus/content/groups/wso2-public/</url>
</pluginRepository>
</pluginRepositories>
<build>
<directory>target/capp</directory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>package</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mvn</executable>
<workingDirectory>${project.build.directory}</workingDirectory>
<arguments>
<argument>clean</argument>
<argument>package</argument>
<argument>-Dmaven.test.skip=${maven.test.skip}</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mvn</executable>
<workingDirectory>${project.build.directory}</workingDirectory>
<arguments>
<argument>clean</argument>
<argument>install</argument>
<argument>-Dmaven.test.skip=${maven.test.skip}</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>deploy</id>
<phase>deploy</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mvn</executable>
<workingDirectory>${project.build.directory}</workingDirectory>
<arguments>
<argument>deploy</argument>
<argument>-Dmaven.test.skip=${maven.test.skip}</argument>
</arguments>
</configuration>
</execution>
</executions>
<configuration />
</plugin>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<buildcommands />
<projectnatures>
<projectnature>org.wso2.developerstudio.eclipse.esb.project.nature</projectnature>
</projectnatures>
</configuration>
</plugin>
<plugin>
<groupId>org.wso2.maven</groupId>
<artifactId>synapse-unit-test-maven-plugin</artifactId>
<version>5.2.38</version>
<executions>
<execution>
<id>synapse-unit-test</id>
<phase>test</phase>
<goals>
<goal>synapse-unit-test</goal>
</goals>
</execution>
</executions>
<configuration>
<server>
<testServerType>${testServerType}</testServerType>
<testServerHost>${testServerHost}</testServerHost>
<testServerPort>${testServerPort}</testServerPort>
<testServerPath>${testServerPath}</testServerPath>
</server>
<testCasesFilePath>${project.basedir}/test/${testFile}</testCasesFilePath>
<mavenTestSkip>${maven.test.skip}</mavenTestSkip>
</configuration>
</plugin>
<plugin>
<groupId>org.wso2.maven</groupId>
<artifactId>wso2-esb-sequence-plugin</artifactId>
<version>2.1.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>sequence</id>
<phase>process-resources</phase>
<goals>
<goal>pom-gen</goal>
</goals>
<configuration>
<artifactLocation>.</artifactLocation>
<typeList>${artifact.types}</typeList>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<configuration />
</plugin>
<plugin>
<groupId>org.wso2.maven</groupId>
<artifactId>wso2-esb-endpoint-plugin</artifactId>
<version>2.1.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>endpoint</id>
<phase>process-resources</phase>
<goals>
<goal>pom-gen</goal>
</goals>
<configuration>
<artifactLocation>.</artifactLocation>
<typeList>${artifact.types}</typeList>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<configuration />
</plugin>
<plugin>
<groupId>org.wso2.maven</groupId>
<artifactId>wso2-esb-api-plugin</artifactId>
<version>2.1.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>api</id>
<phase>process-resources</phase>
<goals>
<goal>pom-gen</goal>
</goals>
<configuration>
<artifactLocation>.</artifactLocation>
<typeList>${artifact.types}</typeList>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<configuration />
</plugin>
<plugin>
<groupId>org.wso2.maven</groupId>
<artifactId>wso2-esb-metadata-plugin</artifactId>
<version>5.2.34</version>
<extensions>true</extensions>
<executions>
<execution>
<id>metadata</id>
<phase>process-resources</phase>
<goals>
<goal>pom-gen</goal>
</goals>
<configuration>
<artifactLocation>.</artifactLocation>
<typeList>${artifact.types}</typeList>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<configuration />
</plugin>
<plugin>
<groupId>org.wso2.maven</groupId>
<artifactId>wso2-esb-task-plugin</artifactId>
<version>2.1.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>task</id>
<phase>process-resources</phase>
<goals>
<goal>pom-gen</goal>
</goals>
<configuration>
<artifactLocation>.</artifactLocation>
<typeList>${artifact.types}</typeList>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<configuration />
</plugin>
<plugin>
<groupId>org.wso2.maven</groupId>
<artifactId>wso2-esb-proxy-plugin</artifactId>
<version>2.1.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>proxy</id>
<phase>process-resources</phase>
<goals>
<goal>pom-gen</goal>
</goals>
<configuration>
<artifactLocation>.</artifactLocation>
<typeList>${artifact.types}</typeList>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<configuration />
</plugin>
</plugins>
</build>
</project>
What am I doing wrong?
If you have a Connector Exporter Project created, you should be able to add the connector as a dependency when creating the Test Suite in the Supportive Artifacts Page.
Another option is to deploy the connector separately before running the Test Suite. You can create a Common project to pack all the common dependencies and have it deployed to MI runtime so it can be referred to by all the other projects.
Jacoco reports for this project contain line coverage for test classes, despite attempting to exclude tests as below
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<configuration>
<excludes>
<exclude>**/*Test.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<configuration>
<excludes>
<exclude>**/*Test.*</exclude>
</excludes>
</configuration>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<configuration>
<excludes>
<exclude>**/*Test.*</exclude>
</excludes>
</configuration>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<excludes>
<exclude>**/*Test.*</exclude>
</excludes>
<element>CLASS</element>
<limits>
<limit implementation="org.jacoco.report.check.Limit">
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.94</minimum>
</limit>
<limit implementation="org.jacoco.report.check.Limit">
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.85</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
The above pom is basically us shotgunning the config with solutions from existing StackOverflow questions, but none of them are working.
Any feedback would be much appreciated!
In absence of Minimal Complete Reproducible Example one can only guess how your test files are placed in your project and why they are shown in report, because jacoco-maven-plugin doesn't show test classes in report by default - here is proof:
for the following pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
src/main/java/Example.java
class Example {
void sayHello() {
System.out.println("Hello, World!");
}
}
and src/test/java/ExampleTest.java
import org.junit.Test;
public class ExampleTest {
#Test
public void test() {
new Example().sayHello();
}
}
execution of
mvn clean verify
generates following report that as you can see doesn't contain src/test/java/ExampleTest.java:
Trying to blindly guess, one can assume that your project has non-standard structure where the above Example.java and ExampleTest.java reside in the same src directory and you have following pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src</sourceDirectory>
<testSourceDirectory>${project.basedir}/src</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
then execution of mvn clean verify indeed will produce following report that contains ExampleTest.java
however in this case following exclusion of **/*Test.*
<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src</sourceDirectory>
<testSourceDirectory>${project.basedir}/src</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
<goal>report</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*Test.*</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
works just fine
I have parent pom which includes all dependencies which will be used by multiple child project. I also configured plugins in parent pom to be extended to child project.
I also wanted the Jacoco code coverage report to be generated and enforce build failure when the coverage is lesser than the minimum threshold.
The Jacoco plugin which fails the build(child project) when configured in child pom but does not fail the build of child project when configured in parent pom.
Parent pom
<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>microservice</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>ms-parent</name>
<description>Parent pom will include all the dependencies commonly used
across the microservices </description>
<properties>
<spring.version>4.3.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>ParentPOM</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<useProjectReferences>false</useProjectReferences>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>
${project.build.directory}/coverage.exec
</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</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>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration><rules><rule>
<element>CLASS</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.99</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.99</minimum>
</limit>
</limits>
<excludes>
</excludes>
</rule></rules></configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Child Pom
<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>
<parent>
<groupId>microservice</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>childmodule</groupId>
<artifactId>module</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>cipher Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- inherits config from parent: can override if required -->
</plugin>
</plugins>
</build>
</project>
The above pom setup does not fail the build of the child project.
If I include the jacoco plugin in child pom then the build fails if the coverage is less than the threshold.
Child Pom with jacoco plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>${project.build.directory}/coverage.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</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>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration><rules><rule>
<element>CLASS</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.99</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.99</minimum>
</limit>
</limits>
<excludes>
</excludes>
</rule></rules></configuration>
</execution>
</executions>
</plugin>
How to ensure the Jacoco configuration set in parent pom fails the build(child project) when the coverage is less than the threshold.
Thanks in advance.
It is very unfortunate that JaCoCo does not provide a way to make an aggregate check for the entire multi-module project. It works for the individual modules only.
If reporting is enough, though, "report-aggregate" goal can be used.
Good example here: jacoco simple integration test solution
Other source:
https://github.com/jacoco/jacoco/wiki/MavenMultiModule
did everything right but still getting missing data file error
${project.build.directory}/coverage-reports/jacoco-it.exec
<build>
<finalName>maven-integration-testing</finalName>
<plugins>
<!-- Used to add source directories to our build. -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<!-- States that the plugin's add-test-source goal is executed at generate-test-sources
phase. -->
<execution>
<id>add-integration-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<!-- Configures the source directory of integration tests. -->
<sources>
<source>src/integration-test/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<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>
<!-- 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>
<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>
</plugin>
<!-- Used for integration tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.15</version>
<executions>
<!-- Ensures that both integration-test and verify goals of the Failsafe
Maven plugin are executed. -->
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<!-- Sets the VM argument line used when integration tests are run. -->
<!-- <argLine>${failsafeArgLine}</argLine> -->
<argLine>${argLine} -Xmx4096m -XX:MaxPermSize=512M ${failsafeArgLine}</argLine>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
prepare-agent : it is not creating the data file , i have spring mvc application which has services and i have written the test cases for that and trying to get the code coverage for this
I've a maven project which use surefire plugin for both unit tests and integration tests.
Is there a maven command that I can use just to skip the unit tests but to run integration tests.
I've tried mvn clean install -DskipUnitTests but that is not working
I would say: no.
But I recommend you the failsafe plugin solution. Some refactoring/renaming, and you can test.
See here my answer, it could help you: How can I fire integration tests separately using failsafe-plugin?
run this command:mvn verify -Dtest=!*Test -DfailIfNoTests=false
Below is my pom.xml
<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>com.aaa</groupId>
<artifactId>IntegrationTestMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>IntegrationTestMaven</name>
<!-- <properties>
Only unit tests are run by default.
<skip.integration.tests>true</skip.integration.tests>
<skip.unit.tests>false</skip.unit.tests>
</properties>
-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>process-resources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/integrationtest/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<!-- Skips unit tests if the value of skip.unit.tests property is true -->
<!-- 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>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
<configuration>
<!-- Skips integration tests if the value of skip.integration.tests property is true -->
<!-- <skipTests>${skip.integration.tests}</skipTests> -->
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- <profiles>
<profile>
<id>all-tests</id>
<properties>
<build.profile.id>all-tests</build.profile.id>
All tests are run.
<skip.integration.tests>false</skip.integration.tests>
<skip.unit.tests>false</skip.unit.tests>
</properties>
</profile>
<profile>
<id>dev</id>
</profile>
<profile>
<id>integration-tests</id>
<properties>
Used to locate the profile specific configuration file.
<build.profile.id>integration-test</build.profile.id>
Only integration tests are run.
<skip.integration.tests>false</skip.integration.tests>
<skip.unit.tests>true</skip.unit.tests>
</properties>
</profile>
</profiles>
-->
</project>