1 introduction

Unit tests are an important part of code quality, but how do you measure how well you write a unit test? Coverage is an important indicator. JaCoCo is a tool for testing Java Code Coverage.

This article will show you how to integrate JaCoCo in a Maven project and demonstrate it in SonarQube. SonarQube installation can be found in this article:

Docker builds Code inspection platform SonarQube and tests Maven Project

2 Basic Concepts

The coverage here refers to the coverage of the test code. This index can be calculated in a variety of ways. The most common ones are as follows:

  • Line coverage: number of lines of code executed/total lines of code, judging how many lines of code are executed by the test;

  • Class coverage: total number of classes executed/code;

  • Branch coverage: the number of logical branches/total branches executed, generally used to check if lf/else both have test coverage;

  • Method coverage: number of methods executed/total number of methods in code, detection for missing methods, constructor also counted as methods.

  • Cyclomatic complexity: Complex programs used to judge code structure, JaCoCo does not consider exception handling branches; It is generally believed that cyclomatic complexity is greater than 10, there is a relatively large risk, the strict requirements should not be greater than 15.

Color identification:

JaCoCo uses color to indicate code coverage, making it easy to see. Red means not covered, green means covered, and yellow means partially covered.

Mode of execution:

There are several ways to perform JaCoCo:

(1) directly from the command execution: www.eclemma.org/jacoco/trun…

(2) Ant: www.eclemma.org/jacoco/trun…

(3) the Maven: www.eclemma.org/jacoco/trun…

(4) Integrated IDE execution: www.eclemma.org/

We’ll focus on maven’s approach next.

3 the maven integration

3.1 Basic Integration

Maven also makes it easy to integrate JaCoCo with the following configuration:

<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>${jacoco.version}</version>  <configuration> <skip>${maven.test.skip}</skip> <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile> <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile> <output>file</output> <append>true</append> <excludes> <exclude>com/pkslow/basic/containsperformance/**</exclude> <exclude>com/pkslow/basic/ReadPropertiesFile</exclude> </excludes> </configuration> <executions> <execution> <id>jacoco-initialize</id> <goals> <goal>prepare-agent</goal> </goals> <phase>test-compile</phase> </execution> <execution> <id>jacoco-site</id> <phase>verify</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>Copy the code

Target /coverage-reports/ Jacoco-ununit. exec will be generated by MVN clean test, but this is unreadable, Sonar is readable. Intellij Idea can also be read by following Run–Show Code Coverage Data.

MVN Clean Verify generates a target/site/ Jacoco/report in a variety of formats that can be easily viewed by opening the index.html file in a browser. As shown below:

3.2 Selection Range

Specify classes that do not perform checks:

<excludes>
  <exclude>com/pkslow/basic/containsperformance/**</exclude>
  <exclude>com/pkslow/basic/ReadPropertiesFile</exclude>
</excludes>Copy the code

3.3 Rules and Thresholds

The Rules tag can specify check thresholds, such as class coverage that must be 100%. The configuration is as follows:

< rules > < rule implementation = "org. Jacoco. Maven. RuleConfiguration" > < element > BUNDLE < / element > < limits > < limit implementation="org.jacoco.report.check.Limit"> <counter>METHOD</counter> <value>COVEREDRATIO</value> < minimum > 0.50 < / minimum > < / limit > < limit implementation = ". Org. Jacoco report. Check. Limit "> < counter > BRANCH > < / counter < value > COVEREDRATIO < value > / < minimum > 0.50 < / minimum > < / limit > < limit implementation = ". Org. Jacoco report. Check. Limit "> <counter>CLASS</counter> <value>MISSEDCOUNT</value> <maximum>0</maximum> </limit> </limits> </rule> </rules>Copy the code

The following check is required to perform the rule verification:

<execution>
  <id>check</id>
  <goals>
    <goal>check</goal>
  </goals>
</execution>Copy the code

If the criteria are not met, the Maven build will fail. However, if we integrate SonarQube, we will set this rule and threshold through SonarQube.

4 Submit to Sonar

Add SonarQube configuration information as follows:

(1) Configure database information

<profiles> <profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.jdbc.url>jdbc:postgresql://localhost/sonar</sonar.jdbc.url> <sonar.jdbc.driver>org.postgresql.Driver</sonar.jdbc.driver> <sonar.jdbc.username>user</sonar.jdbc.username> <sonar.jdbc.password>password</sonar.jdbc.password> <sonar.host.url>http://localhost:9000</sonar.host.url> </properties>  </profile> </profiles>Copy the code

(2) Configure the user name and password

<profiles>
  <profile>
    <id>sonar</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <sonar.host.url>http://localhost:9000</sonar.host.url>
      <sonar.login>admin</sonar.login>
      <sonar.password>admin</sonar.password>
    </properties>
  </profile>
</profiles>Copy the code

(3) Configure the token

<profiles>
  <profile>
    <id>sonar</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <sonar.host.url>http://localhost:9000</sonar.host.url>
      <sonar.login>9656c84090b2481db6ea97b6d14d87d546bff619</sonar.login>
    </properties>
  </profile>
</profiles>Copy the code

After the configuration is complete, run the following command:

mvn clean verify sonar:sonarCopy the code

If you do not want to add a configuration, run the following command to specify the configuration:

mvn clean verify sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=9656c84090b2481db6ea97b6d14d87d546bff619Copy the code

5 concludes

JaCoCo is important for project quality management and should be used. Eventually maven pom configuration file. The XML rows is too big, please go to (www.pkslow.com/archives/ma)… Reference.


Visit pumpkin Talk www.pkslow.com for more exciting articles!

Welcome to pay attention to the wechat public number “Pumpkin slow Talk”, will continue to update for you…

Read more and share more; Write more. Organize more.