capint
7/23/2017 - 12:32 PM

Java >> Jacoco >> Tutorial

Java >> Jacoco >> Tutorial

Source: http://www.javaworld.com/article/2074515/core-java/unit-test-code-coverage-with-maven-and-jacoco.html
Principle: Attach a Jacoco agent to a JVM when it starts -> The results file (.exec) is created when the JVM terminates

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.5.5.201112152213</version>
  --> define where the jacoco reports are output
  <configuration>
   <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
   <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
  </configuration>
  <executions>
    --> make the agent run before the tests are run
   <execution>
    <id>jacoco-initialize</id>
    <goals>
     <goal>prepare-agent</goal>
    </goals>
   </execution>
   --> make sure that the jacoco report task is run when package is executed
   <execution>
    <id>jacoco-site</id>
    <phase>package</phase>
    <goals>
     <goal>report</goal>
    </goals>
   </execution>
  </executions>
 </plugin>