user1365697
user1365697

Reputation: 5989

Why is JaCoCo Maven Plugin skipping the report?

I have unit test and I want to get the code coverage result in a report. When I run the tests with Maven, I have in the logs:

[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-unit-test) @ che-core-api-git ---
[INFO] Skipping JaCoCo execution due to missing execution data file:C:\dev\eclipse_che_core\platform-api\che-core-api-git\target\coverage-reports\jacoco-ut.exec

This is the relevant part of my POM.

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <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>${project.build.directory}/coverage-reports/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}/coverage-reports/jacoco-ut.exec</dataFile>
        <!-- Sets the output directory for the code coverage report. -->
        <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

Why is the plugin skipping the execution? How can I fix this?

Upvotes: 1

Views: 3736

Answers (1)

Tunaki
Tunaki

Reputation: 137094

The first step in having a code coverage report is to start the JaCoCo agent, which is done by invoking the prepare-agent goal of the plugin. The purpose of this goal is:

Prepares a property pointing to the JaCoCo runtime agent that can be passed as a VM argument to the application under test. Depending on the project packaging type by default a property with the following name is set:

  • tycho.testArgLine for packaging type eclipse-test-plugin and
  • argLine otherwise.

Your current configuration correctly sets a destFile, that is later used by the report goal and its dataFile parameter.

The problem is that the property pointing to the JaCoCo agent was not correctly used. Your configuration of the JaCoCo Maven Plugin sets the propertyName parameter to surefireArgLine with

<propertyName>surefireArgLine</propertyName>

This means that JaCoCo will store the path to its runtime agent in this property, and that when the Surefire Plugin is invoking the tests, this property will need to be added to the VM arguments. However, the Surefire Plugin does not use the surefireArgLine to add VM arguments during the tests; more specifically, the right parameter is called argLine:

Arbitrary JVM options to set on the command line.

Therefore, the property surefireArgLine that JaCoCo set pointing to its agent is unused during the tests. To correct this, it is possible to configure the Surefire Plugin to consider this new property:

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <configuration>
    <argLine>${surefireArgLine}</argLine>
  </configuration>
</plugin>

This will tell the Surefire Plugin to use the new property surefireArgLine as VM arguments when launching the tests. With this change, you will see in the logs:

[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:report (post-unit-test) @ test ---
[INFO] Analyzed bundle 'test' with 3 classes

Do note that, by default, none of this is needed: as said previously, JaCoCo stores by default this VM argument in the argLine property, which is exactly the name of the Surefire Plugin parameter used to inject custom VM arguments. As such, another solution would be to just remove your

<propertyName>surefireArgLine</propertyName>

configuration element, and let the defaults kick in.

Upvotes: 1

Related Questions