12 Aralık 2021 Pazar

jacoco plugin - Java Code Coverage İçindir

Giriş
Code coverage verisi üretir. Açıklaması şöyle. Yani JVM kapanınca Jacoco bir dosya üretir. Bu dosyanın yolu şöyle target/jacoco-ut.exec
Jacoco comes in 2 parts:
1. Jacoco supplies a Java agent which attaches to the JVM of the running application and records all the classes/lines executed. Once the JVM terminates, the agent generates an exec file, which contains the coverage report of everything executed in the JVM’s lifetime.

2. the Jacoco plugin takes the coverage report and generates a human readable report, with a filter to exclude any libraries or dependencies.
jacoco goal listesi şöyle
help
prepare-agent
prepare-agent-integration
merge
report
report-integration
report-aggregate
check
dump
instrument
restore-instrumented-classes
prepare-agent goal
Açıklaması şöyle
The prepare-agent goal prepares the JaCoCo runtime agent to record the execution data. It records the number of lines executed, backtraced, etc. By default, the execution data is written to the file target/jacoco-ut.exec.
report goal
Açıklaması şöyle
The report goal creates code coverage reports from the execution data recorded by the JaCoCo runtime agent. Since we have specified the phase property, the reports will be created after the compilation of the test phase. By default, the execution data is read from the file target/jacoco-ut.exec, and the code coverage report is written to the directory target/site/jacoco/index.html.
Jacoco Çalışmasın istersek
Şöyle yaparız
<project>
  <properties>
    <jacoco.skip>true</jacoco.skip>
  </properties>
</project>
Komut Satırından Agent'ı Çalıştırmak

Maven İle Plugin'i Çalıştırmak
Örnek
maven ile sadece bu plugin'i çalıştırmak için şöyle yaparız.
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent test 
  -Dmaven.test.failure.ignore=true
Örnek
Testleri koşup rapor çıkartmak için şöyle yaparız
mvn test jacoco:report
Şu satırı projects altına ekleriz
<reporting>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
        <reportSets>
          <reportSet>
            <reports>
            <!-- select non-aggregate reports -->
              <report>report</report>
            </reports>
          </reportSet>
        </reportSets>
    </plugin>
  </plugins>
</reporting>
Örnek
maven ile verify safhasında bu plugin'i de çalıştırmak için şöyle yaparız.
mvn clean verify
1. En Basit (bare minimum) Kullanım
En Basit (bare minimum) Kullanım yazısına taşıdım

2. Konfigürasyon İle Kullanım
configuration tag global plugin veya her bir goal için tanımlanabilir

destFile tag
Açıklaması şöyle
The destFile tag is used for setting the path to the file containing the execution data.
propertyName-surefireArgLine tag
Açıklaması şöyle
This tag sets the name of the property that contains the settings for the JaCoCo runtime agent. This also sets the VM argument line when the unit tests are run.
dataFile tag
Açıklaması şöyle
The dataFile tag is used to set the path to the file containing execution data.
outputDirectory  tag
Açıklaması şöyle
This tag sets the output directory for the code coverage reports.
Örnek - destFile
Şöyle yaparıztarget/jacoco.exe dosyası üretilir. target/site/jacoco/index.html dosyası üretilir.
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.3</version>
  <configuration>
    <destFile>${jacoco.reportPath}</destFile>
    <append>true</append>
  </configuration>
  <executions>
    <execution>
      <id>coverage-initialize</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
    <execution>
      <id>coverage-report</id>
      <goals>
        <goal>report</goal>
      </goals>
    </execution>
  </executions>
</plugin>
Örnek
Şöyle yaparız
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.6</version>
  <executions>
    <execution>
      <id>prepare-agent</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
      <configuration>
        <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
        <propertyName>surefireArgLine</propertyName>
      </configuration>
    </execution>
    <execution>
      <id>report</id>
      <phase>test</phase>
      <goals>
        <goal>report</goal>
       </goals>
       <configuration>
        <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
        <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>
Örnek - destFile + dataFile + outputDirectory
Şöyle yaparız. Coverage dosyasının yaratılacağı yer belirtiliyor. Report aşamasında bu dosya girdi olarak kullanılıyor
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.5</version>
  <executions>
    <execution>
      <id>pre-test</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
      <configuration>
        <propertyName>jacocoArgLine</propertyName>
        <destFile>${project.test.result.directory}/jacoco/jacoco.exec</destFile>
      </configuration>
    </execution>
    <execution>
      <id>post-test</id>
      <phase>test</phase>
      <goals>
        <goal>report</goal>
      </goals>
      <configuration>
        <dataFile>${project.test.result.directory}/jacoco/jacoco.exec</dataFile>
        <outputDirectory>${project.test.result.directory}/jacoco</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>
Örnek - destFile
Şöyle yaparız. 
<properties>
  <jacoco.reportPath>
${project.basedir}/target/cov-reports/jacoco.exec
</jacoco.reportPath>
</properties>
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.2</version>
  <configuration>
    <destFile>${jacoco.reportPath}</destFile>
    <append>true</append>
  </configuration>
  <executions>
    <execution>
      <id>coverage-initialize</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
  </executions>
</plugin>
<properties>
exclude
Açıklaması şöyle
Starting from JaCoCo 0.8.2, we can exclude classes and methods by annotating them with a custom annotation with the following properties:
- The name of the annotation should include Generated.
- The retention policy of annotation should be runtime or class.
Yani ya kendimiz ismi @Generated olan bir anotasyon yazarız ve kullanırız. Şöyle yaparız
@Documented
@Retention(RUNTIME)
@Target({TYPE, METHOD, CONSTRUCTOR})
public @interface Generated {
}
Ya da Lombok kullanıyorsak projenin kök dizininde lombok.config dosyasına şu satırı ekleriz. Böylece Lombok ürettiği tüm kodlara kendisine ait olan @Generated anotasyonunu ekler
lombok.addLombokGeneratedAnnotation = true
Örnek - exclude
Şöyle yaparız
<plugin> 
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>com/baeldung/**/ExcludedPOJO.class</exclude>
            <exclude>com/baeldung/**/*DTO.*</exclude>
            <exclude>**/config/*</exclude>
        </excludes>
     </configuration>
     ...
</plugin>
Örnek
Şöyle yaparız
<plugins>
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <configuration>
      <excludes>
        <exclude>io/truongbn/github/jacoco/JacocoApplication.class</exclude>
      </excludes>
    </configuration>
    ...
  </plugin>
</plugins>

3. Check Goal İle Kullanım

Rapor Çıktısı
Rapor Çıktısı yazısına taşıdım

Hiç yorum yok:

Yorum Gönder

Local Snapshot Kullanmak

Örnek Şöyle yaparız <repository> <id>snapshot-repository</id> <name>Maven2 Snapshot Repository</name> &l...