jacoco code coverage with integration tests and multi module project - java

i am trying to get jacoco working, so it reports me my
code coverage of my integration tests. Currently i have this
setup
parent.pom
-- module1
-- module2
-- module3
-- module4
-- report
module 1 to 3 contains my source code, while module 4 contains a
client, which runs the integration tests.
parent.pom includes the jacoco-plugin:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>prepare-unit-test-agent</id>
<phase>generate-test-sources</phase>
<configuration>
<append>true</append>
<destFile>${project.build.directory}/jacoco.exec</destFile>
</configuration>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>prepare-it-test-agent</id>
<phase>pre-integration-test </phase>
<configuration>
<propertyName>failsafeArgLine</propertyName>
<destFile>${project.build.directory}/jacoco-it.exec</destFile>
<append>true</append>
</configuration>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
</executions>
</plugin>`
report pom:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>it-report</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
<configuration>
<dataFileIncludes>
<dataFileInclude>**/jacoco.exec</dataFileInclude>
<dataFileInclude>**/jacoco-it.exec</dataFileInclude>
</dataFileIncludes>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-overall</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
in my report module, all other modules are imported as dependencie
with scope = compile.
Now, the generated Coverage, only shows that the tests cover takes
place in module 1. Since the logic is something like this: Module 3
= Interface Module 2 = Implementation of the Interfaces Module 1 = entities
it is strange that the Code of Module 2 and 3 are not shows as
covered. All the tests call methods from Module 3.
Just for side info: Source is in Java, Tests are in groovy

Related

Missing content in XML coverage report when merging multiple JaCoCo.exec files into 1 file

While I have successfully managed to have a unique .exec file generated within the build of my multi-module Java project, I have found that the side-effect of it is that the /target/site/jacoco-aggregate/ folder now shows 0% of coverage for all my projects even though the generated mergedJaCoCoReport.exec file contains all the proper coverage information.
I actually still need the xml report files as SonarCloud can't read the *.exec files.
If I remove the action below (this action in the end merges all the files into 1 .exec file) from my pom.xml, then the XML report files are valid again
<configuration>
<destFile>${session.executionRootDirectory}/mergedJaCoCoReport.exec</destFile>
<append>true</append>
</configuration>
Does anyone know if I am missing something here ?
Thanks in advance
<plugin>
<artifactId>jacoco-maven-plugin</artifactId> <groupId>org.jacoco</groupId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${session.executionRootDirectory}/mergedJaCoCoReport.exec</destFile>
<append>true</append>
</configuration>
</execution>
<execution>
<id>all-tests-report</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
<configuration>
<skip>false</skip>
</configuration>
</execution>
<execution>
<id>unit-tests-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<skip>false</skip>
</configuration>
</execution>
</executions>
</plugin>

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.

Generate java code from XSD using maven-jaxb2-plugin where the schemas have dependencies

My goal is to generate a java code from XSD using maven-jaxb2-plugin, and in particular from 3 separate schemas. The problem I am facing is that all schemas have dependency on a common schema and the same classes are generated three times.
The part of the pom I am using is shown below:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>xjc-a</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.a</packageName>
<schemaDirectory>src/main/resources/a</schemaDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
<execution>
<id>xjc-b</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.b</packageName>
<schemaDirectory>src/main/resources/b</schemaDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
<execution>
<id>xjc-c</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.c</packageName>
<schemaDirectory>src/main/resources/c</schemaDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
</executions>
</plugin>
Schemas a, b, c all include a common schema, say d. When the sources are generated class from d are generated in each package and what I wish to do is to factor out the common classes into a separate package.
Thanks

Java - Maven JAXB-2 plugin multiple schemes with different configurations doesn't generate classes

We are moving our existing project from Ant + Eclipse to Maven + IntelliJ IDEA.
I am currently using JAXB to generate classes from xsd files. I want to continue the current project structure so i want jaxb2-maven-plugin to generate the classes in a specific location. I have multiple schemes and want to generate the classes in different locations. I'm using multiple plugin execution bindings in order to do that as instructed in the JAXB-2 Maven plugin site.
My problem is that only the first execution is performed. None of the classes in the second execution are generated.
Here is my POM.xml file relevant part:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>schema1</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
<schemaFiles>myschema1.xsd</schemaFiles>
<packageName>xml</packageName>
<outputDirectory>${basedir}/src/main/java/com/example/dor/a</outputDirectory>
<arguments>-extension -Xcloneable -Xdefault-value -Xsetters -Xannotate</arguments>
<staleFile>${build.directory}/.jaxb-staleFlag-1</staleFile>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
<execution>
<id>schema2</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
<schemaFiles>myschema2.xsd</schemaFiles>
<packageName>xml</packageName>
<outputDirectory>${basedir}/src/main/java/com/example/dor/b</outputDirectory>
<arguments>-extension -Xcloneable -Xdefault-value -Xsetters -Xannotate</arguments>
<staleFile>${build.directory}/.jaxb-staleFlag-1</staleFile>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
</executions>
</plugin>
An update answer using version 2.5.0 of the plugin. This would be the configuration:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>schema1</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.your.package</packageName>
<sources>
<source>${project.basedir}/src/main/resources/xsd/sample1/sample1.xsd</source>
</sources>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
<execution>
<id>schema2</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.your.package</packageName>
<sources>
<source>${project.basedir}/src/main/resources/xsd/sample2/sample2.xsd</source>
</sources>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
</executions>
</plugin>
Hope it helps for newer configurations.
I would upgrade to 1.6, and you will have to put the 2 schemas in different packages to stop a conflict in the generated ObjectFactory. Below works for me:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>schema1</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
<schemaFiles>myschema1.xsd</schemaFiles>
<packageName>xml.a</packageName>
<outputDirectory>${basedir}/src/main/generated1</outputDirectory>
<clearOutputDir>true</clearOutputDir>
</configuration>
</execution>
<execution>
<id>schema2</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
<schemaFiles>myschema2.xsd</schemaFiles>
<packageName>xml.b</packageName>
<outputDirectory>${basedir}/src/main/generated2</outputDirectory>
<clearOutputDir>true</clearOutputDir>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Had the same problem.
Enabled the -X option of maven to see why the second generation is not performed.
The second generation was not run because the staleFile was the same. I had to add to both executions the parameter staleFile having different values:
<staleFile>${project.build.directory}/jaxb2/.xjcStaleFlag1</staleFile>
........
<staleFile>${project.build.directory}/jaxb2/.xjcStaleFlag2</staleFile>

Categories

Resources