# 情境分析
今天在为项目集成sonar单元测试覆盖率的过程中,发现了许多问题,在这里记录下,为大家提供一下解决思路


# 单测覆盖率执行思路
1. 首先通过maven打包,然后生成测试文件
2. sonar扫描导入maven生成的测试文件,出现单测覆盖率


# 可能出现问题的点
1. 可能没有生成测试文件
2. 生成的测试文件可能为空,没有数据


# 上面两个问题,如何确定呢? (通过查看日志)
1. 使用 Surefire 配置破坏 JaCoCo 代理
在maven install打包的日志中,出现如下日志信息
[INFO] Skipping JaCoCo execution due to missing execution data file.

或者,不会出现如下信息

[INFO] --- jacoco-maven-plugin:0.8.5:prepare-agent (default-prepare-agent) @ sirius ---
[INFO] surefireArgLine set to -javaagent:/builds/situ/situ-java-program/situ-galaxy/sir-server/sir/./.m2/repository/org/jacoco/org.jacoco.agent/0.8.5/org.jacoco.agent-0.8.5-runtime.jar=destfile=/builds/situ/situ-java-program/situ-galaxy/sir-server/sir/target/jacoco.exec,includes=com/situ/sir/**/*,excludes=**/po/**/*:**/config/**/*

2. 虽然单个测试类可以运行也没有错,但是maven install打包的时候,run的数字为0
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
# pom文件示例配置 (第一种)

<properties>
<!-- sonar单测扫描 -->
<jacoco.version>0.8.5</jacoco.version>
<jacoco.reportPath>${project.basedir}/target/jacoco-ut.exec</jacoco.reportPath>
</properties>

<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>${jacoco.version}</version>
<classifier>runtime</classifier>
</dependency>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>${jacoco.reportPath}</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<includes> <!--覆盖率计算包含的类-->
<include>com/cc/situ/**/*</include>
</includes>
<excludes>
<exclude>**/po/**/*</exclude>
<exclude>**/config/**/*</exclude>
</excludes>
</configuration>

<executions>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal><!--生成覆盖率文件和报告-->
</goals>
<configuration>
<dataFile>${jacoco.reportPath}</dataFile>
</configuration>
</execution>
</executions>
</plugin>

如果你按照第一种不行,在日志中出现错误情况的第一种

那么可能是Surefire 配置破坏 JaCoCo 代理

请采用第二种方案

# pom文件示例配置 (第二种)

<properties>
<!-- sonar单测扫描 -->
<jacoco.version>0.8.5</jacoco.version>
<jacoco.reportPath>${project.basedir}/target/jacoco-ut.exec</jacoco.reportPath>
</properties>

<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>${jacoco.version}</version>
<classifier>runtime</classifier>
</dependency>


<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<includes> <!--覆盖率计算包含的类-->
<include>com/situation/sirius/**/*</include>
</includes>
<excludes> <!--排除像Pojo、配置等无单元测试意义的类。如有,在Sonar服务端也需要配合排除(Ignore Code Coverage: https://docs.sonarqube.org/latest/project-administration/narrowing-the-focus/#header-5)-->
<exclude>**/po/**/*</exclude>
<exclude>**/config/**/*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal><!--生成覆盖率文件和报告-->
</goals>
<configuration>
<dataFile>${jacoco.reportPath}</dataFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
<forkMode>once</forkMode>
<reuseForks>true</reuseForks>
<testFailureIgnore>true</testFailureIgnore>
<!--suppress UnresolvedMavenProperty -->
<argLine>-Dfile.encoding=UTF-8 ${surefireArgLine}</argLine>
<systemPropertyVariables>
<jacoco-agent.destfile>${jacoco.reportPath}</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>

请注意maven依赖也会影响,是否能够检测出单测覆盖率

# 请使用第一个,不要使用第二个

1. 第一个
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${springboot.version}</version>
</dependency>

2. 第二个
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>${springboot.version}</version>
</dependency>