3

I have a maven project (link) and I want to run code coverage on it.

I ran the command mvn test -Pcoverage jacoco:prepare-agent jacoco:report on the main project pom file, but the report is not generated. Instead I get a warning saying

[INFO] --- jacoco-maven-plugin:0.7.7.201606060606:report (post-test) @ pulsar-discovery-service ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.7.201606060606:prepare-agent (default-cli) @ pulsar-discovery-service ---
[INFO] argLine set to -javaagent:/home/jai1/.m2/repository/org/jacoco/org.jacoco.agent/0.7.7.201606060606/org.jacoco.agent-0.7.7.201606060606-runtime.jar=destfile=/home/jai1/pulsar/pulsar-discovery-service/target/jacoco.exec
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.7.201606060606:report (default-cli) @ pulsar-discovery-service ---
[INFO] Skipping JaCoCo execution due to missing execution data file.

Can someone suggest how can I generate the code coverage report with this pom file. I am using apache-maven-3.3.9 and testNG.

juherr
  • 5,640
  • 1
  • 21
  • 63
user1918858
  • 1,202
  • 1
  • 20
  • 29
  • This might be helpful. http://stackoverflow.com/questions/18107375/getting-skipping-jacoco-execution-due-to-missing-execution-data-file-upon-exec – Ravi Apr 19 '17 at 23:16
  • @Ravi - Nope didn't help – user1918858 Apr 19 '17 at 23:28
  • Please show your `maven-surefire-plugin` and `jacoco-maven-plugin` configuration. Also, does your `coverage` profile contain a dependency on `sonar-jacoco-listeners`? – Steve C Apr 20 '17 at 00:06
  • Entire configuration is in https://github.com/yahoo/pulsar/blob/master/pom.xml – user1918858 Apr 20 '17 at 00:27

4 Answers4

5

Your pom.xml contains

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <argLine> -Xmx2G -XX:MaxDirectMemorySize=8G
        -Dio.netty.leakDetectionLevel=advanced</argLine>
    </configuration>
  </plugin>

while JaCoCo documentation for prepare-agent goal states

If your project already defines VM arguments for test execution, be sure that they will include property defined by JaCoCo.

... define "argLine" as a Maven property rather than as part of the configuration of maven-surefire-plugin:

  <properties>
    <argLine>-your -extra -arguments</argLine>
  </properties>
  ...
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <!-- no argLine here -->
    </configuration>
  </plugin>
Godin
  • 9,801
  • 2
  • 39
  • 76
0
It seems like you are missing below line in pom.xml 
<destFile>
   ${project.build.directory}/coverage-reports/jacoco-unit.exec
</destFile>

Please have look at the below pom :

 <plugin>
           <groupId>org.jacoco</groupId>
           <artifactId>jacoco-maven-plugin</artifactId>
           <version>0.7.1.201405082137</version>
            <executions>
                  <execution>
                      <id>prepare-jacoco-4-unit-tests</id>
                      <goals>
                           <goal>prepare-agent</goal>
                      </goals>
                      <configuration>
          <!-- Sets the path to the file which contains the execution data. -->
         <destFile>
             ${project.build.directory}/coverage-reports/jacoco-unit.exec
         </destFile>                                             
         <propertyName>jacocoArgLine</propertyName>
         </configuration>
         </execution>

            <execution>
                <id>create-jacoco-reports-4-unit-tests</id>
                <phase>prepare-package</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-unit.exec
          </dataFile>
          </configuration>
          </execution>
  </executions>
  </plugin>

And then in maven-surefire-plugin following should be defined :

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.17</version>
     <configuration>
         <skipTests>${skipUnitTests}</skipTests>
         <argLine>${jacocoArgLine}</argLine>
     </configuration>
</plugin>
Sagar Jani
  • 257
  • 1
  • 10
  • Tried what you said in "https://github.com/jai1/pulsar/tree/temp" but still no luck – user1918858 Apr 20 '17 at 02:49
  • ok, let me try to clone your project and try locally. Thanks. – Sagar Jani Apr 20 '17 at 03:31
  • thanks for your time Just a heads up - the test suite is pretty huge so delete the tests (or keep just one) before running the mvn job else it will take two hours to complete. find . -name "*Test.java" | xargs rm -f – user1918858 Apr 20 '17 at 06:14
0

The following is what works for us:

Set up some properties:

<properties>
    ...
    <!-- versions below are last ones that are Java 6 compatible -->
    <version.jacoco-agent>0.7.4.201502262128</version.jacoco-agent>
    <jacocoArgLine />
    <surefire.base.argLine>-XX:-UseSplitVerifier -XX:MaxPermSize=384m -Xmx1024m -Djava.awt.headless=true</surefire.base.argLine>
    ...
</properties>

and the coverage profile:

    <profile>
        <id>coverage</id>
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>${surefire.plugin.version}</version>
                        <configuration>
                            <argLine>${surefire.base.argLine} @{jacocoArgLine}</argLine>
                            <systemPropertyVariables>
                                <java.awt.headless>true</java.awt.headless>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>prepare-agent</goal>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
Steve C
  • 18,876
  • 5
  • 34
  • 37
0

See below a complete simple demo project showing unit, integration, cucumber tests. The test coverage is shown via Jacoco.

  • Multi module project
  • Unit test (via mvn clean install)
  • Integration test (via mvn clean install -P integration-test)
  • Cucumber tests (via mvn clean install) - to demo unit/it-testing
  • Jacoco - test coverage ( both aggregate datafile and aggregate reporting)
  • FindBugs - code quality

Enjoy the sipmle demo project.

tm1701
  • 7,307
  • 17
  • 79
  • 168
  • Nice Try - but Failed to execute goal on project module3: Could not resolve dependencies for project nl.deholtmans:module3:jar:1.0-SNAPSHOT: Could not find artifact nl.deholtmans:module1:jar:1.0-SNAPSHOT I'm not up or downvoting – Wolfgang Fahl Jun 26 '19 at 12:21