Maven combines JaCoCo Co and Sonar to see if you've written enough tests

1 Introduction Unit testing is an important p...
3.1 Basic Integration
3.2 Selection Range
3.3 Rules and Thresholds
1 Introduction

Unit testing is an important part of ensuring code quality. How do you measure how well a unit test is written?Coverage is an important indicator.JaCoCo CoCois a tool specifically designed for Java to detect test coverage, which is called Java Code Coverage in English.

This article explains how to integrate JaCoCo in the Maven project and show it in SonarQube.SonarQube installation can refer to this article:

<Docker builds a code detection platform, SonarQube, and detects maven projects>

2 Basic concepts

Coverage here refers to test code coverage, which can be calculated in a variety of ways, such as the following:

  • Line coverage: the number of lines of code executed / the total number of lines of code executed to determine how many lines of code were tested for execution;

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

  • Branch Coverage: The number of logical branches executed/total branches, typically used to detect if lf/else has test coverage;

  • Method Coverage: The number of methods executed / the total number of methods coded, whether a method has been omitted, and the construction method as a method.

  • Cycle Complexity: A complex program used to determine the structure of code that JaCoCo Co does not consider the branch of exception handling; it is generally assumed that if the cycle complexity is greater than 10, there is a greater risk and it is strictly required that it not be greater than 15.

Color identification:

JaCoCo Co uses color to identify code coverage at a glance.Red means no coverage, green means covered, and yellow means partial coverage.

Execution method:

There are several ways to execute JaCoCo:

(1) Execute directly by command: https://www.eclemma.org/jacoco/trunk/doc/agent.html

(2) Ant execution: https://www.eclemma.org/jacoco/trunk/doc/ant.html

(3) Maven Execution: https://www.eclemma.org/jacoco/trunk/doc/maven.html

(4) Integrated IDE implementation: https://www.eclemma.org/

Next we will focus on how maven works.

3 maven integration

3.1 Basic Integration

It's also easy for Maven to integrate JaCoCo Co with the following configurations:

<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>$</version> <configuration> <skip>$</skip> <destFile>$/target/coverage-reports/jacoco-unit.exec</destFile> <dataFile>$/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>

Executing mvn clean test results in a report target/coverage-reports/jacoco-Unit.execBut this is human unreadable, Sonar readable.Intellij Idea can also be read, just open it according to Run--Show Code Coverage Data.

Executing mvn clean verify generates a report target/site/jacoco/, which has multiple formats and is opened in a browserIndex.htmlThe file is easy to view.As shown in the following figure:

3.2 Selection Range

Specify that some classes do not perform detection:

<excludes> <exclude>com/pkslow/basic/containsperformance/**</exclude> <exclude>com/pkslow/basic/ReadPropertiesFile</exclude> </excludes>

3.3 Rules and Thresholds

The Rules tag can specify check thresholds, such as class coverage that must be 100%.Inside the configuration Configure the following:

<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>

The following check is required to perform this rule check:

<execution> <id>check</id> <goals> <goal>check</goal> </goals> </execution>

If the condition is not met, maven build will fail.However, if we integrate SonarQube, we set this rule and threshold through SonarQube.

4 Submit to Sonar

There are three ways to add SonarQube configuration information:

(1) Configuration 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>

(2) Configure user name 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>

(3) Configuration 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>

All three of the above can be done. When the configuration is complete, execute the command as follows:

mvn clean verify sonar:sonar

If you do not want to add a configuration, you can specify it directly by command as follows:

mvn clean verify sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=9656c84090b2481db6ea97b6d14d87d546bff619
5 Summary

JaCoCo plays an important role in project quality management and should be used.Final maven profilePom.xmlThe number of lines is too large, please go to ( https://www.pkslow.com/archives/maven-jacoco-sonar ) Reference.

Welcome to Slow-talk pumpkinWww.pkslow.com Get more great articles!

Welcome to the WeChat Public Number <Pumpkin Slow>, it will be updated for you continuously.

Read more, share more; write more, organize more.

20 May 2020, 12:50 | Views: 1980

Add new comment

For adding a comment, please log in
or create account

0 comments