Analyzing code coverage with the Maven JaCoCo plugin

JaCoCo is a free Java code coverage tool. This is essentially the successor to Emma, and it has been developed by the EclEmma team as an Eclipse project.

JaCoCo offers line and branch coverage.

Getting ready

Maven is set up on your system and is verified to work. To do this, refer to the first three recipes of Chapter1, Getting Started.

How to do it...

Use the following steps to analyze the code coverage with the Maven JaCoCo plugin:

  1. Open the pom file of a project that has unit tests (for instance, project-with-tests).
  2. Add the following code:
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.7.2.201409121644</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>
       </executions>
     </plugin>
  3. Run the following command from the command prompt:
    mvn package
    
  4. Note the output for the preceding command:
    [INFO] --- jacoco-maven-plugin:0.7.2.201409121644:prepare-agent (default-prepare-agent) @ project-with-tests ---
    [INFO] argLine set to -javaagent:C:\software\maven\org\jacoco\org.jacoco.agent\0.7.2.201409121644\org.jacoco.agent-0.7.2.201409121644-runtime.jar=destfile=C:\projects\apache-maven-cookbook\project-with-tests\target\jacoco.exec
    [INFO] --- jacoco-maven-plugin:0.7.2.201409121644:report (default-report) @ project-with-tests ---
    [INFO] Analyzed bundle 'Project with Tests with 1 classes
    
  5. Open the index.html file generated in the target/site/jacoco folder:
    How to do it...

How it works...

In the pom file, we instruct Maven to run the following two goals of the Maven JaCoCo plugin:

  • prepare-agent: This is bound by default to the initialize phase of the Maven default lifecycle. The goal runs and prepares the agent that does the analysis.
  • report: This agent gathers test coverage information when the tests are run and creates the report as part of the prepare-package phase (which we have explicitly specified).

The report gives information about the test coverage. Green indicates lines that are covered by tests and red indicates lines that are not covered by tests. In the preceding example, 12 of 19 instructions are not covered by tests.

There's more...

You could subject the project to code coverage and generate the same report without making any changes to the pom file. To do this, run the following command:

mvn jacoco:prepare-agent test jacoco:report

Now, you may get the following error:

[ERROR] No plugin found for prefix 'jacoco' in the current project and in the plugin groups [org.apache.maven.plugins] available from the repositories [local (C:softwaremaven), central (https://repo.maven.apache.org/maven2)] -> [Help 1]

To fix this, specify the groupId and artifactId parameters of the plugin explicitly. In the Configuring Maven to search for plugins recipe of Chapter 8, Handling Typical Build Requirements, we will see an alternate way to address this.

In the following code, what what we will be doing is explicitly calling the relevant goals that we saw getting executed earlier. So, first prepare-agent will run, followed by test, and then the report goal:

mvn org.jacoco:jacoco-maven-plugin:prepare-agent test org.jacoco:jacoco-maven-plugin:report

How about failing the build if the code coverage is below a threshold value? To do this, perform the following steps:

  1. Add the following execution block to the plugin configuration in the build section specified earlier:
    <execution>
        <id>default-check</id>
        <phase>prepare-package</phase>
        <goals>
            <goal>check</goal>
        </goals>
        <configuration>
            <rules>
                <rule>
                    <element>BUNDLE</element>
                    <limits>
                        <limit>
                            <counter>COMPLEXITY</counter>
                            <value>COVEREDRATIO</value>
                            <minimum>0.60</minimum>
                        </limit>
                    </limits>
                </rule>
            </rules>
        </configuration>
    </execution>
  2. Run the following command:
    mvn package
    
  3. Observe the result as shown in following screenshot:
    There's more...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.111