Mockito测试SonarQube代码覆盖率0%及测试报错问题求助
Jenkins+Mockito+SonarQube问题排查与修复
问题描述
通过Jenkins Pipeline开发了若干Mockito测试,期望通过SonarQube查看测试报告,但遇到以下问题:
测试执行报错:为避免数据库连接异常,在构建制品前添加了清理命令,但11个测试全部报错(推测CentOS机器未部署数据库),报错信息:
[ERROR] Tests run: 11, Failures: 0, Errors: 11, Skipped: 0SonarQube覆盖率异常:添加测试后,SonarQube中代码覆盖率仍为0%,代码行数无变化。
当前Jenkins脚本配置
sh ' mvn sonar:sonar -Dsonar.sources=src/main/java -Dsonar.css.node=. -Dsonar.java.binaries=. -Dsonar.host.url=http://192.168.2.2:9000/ -Dsonar.login=admin -Dsonar.password=sonar -Dsonar.jacoco.reportPath=build/reports/jacoco.xml'
构建前清理命令
sh 'mvn package -Dmaven.test.skip=true -P test-coverage'
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.3</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>uk.un.ts</groupId> <artifactId>proj</artifactId> <version>1.0</version> <name>achat</name> <description>Projet pour le module DevOps</description> <properties> <java.version>1.8</java.version> <maven-jar-plugin.version>3.1.0</maven-jar-plugin.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jacoco.version>0.8.6</jacoco.version> <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin> <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis> <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath> <sonar.language>java</sonar.language> </properties> <distributionManagement> <snapshotRepository> <id>nexus-snapshots</id> <url>http://192.168.2.2:8081/repository/maven-snapshots/</url> </snapshotRepository> <repository> <id>nexus-releases</id> <url>http://192.168.2.2:8081/repository/maven-releases/ </url> </repository> </distributionManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.9.1</version> </dependency> <dependency> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>${jacoco.version}</version> <executions> <execution> <id>jacoco-initialize</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>jacoco-site</id> <phase>package</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>${maven-deploy-plugin.version}</version> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.sonatype.plugins</groupId> <artifactId>nexus-staging-maven-plugin</artifactId> <executions> <execution> <id>default-deploy</id> <phase>deploy</phase> <goals> <goal>deploy</goal> </goals> </execution> </executions> <configuration> <serverId>nexus</serverId> <nexusUrl>http://192.168.2.2:8081/</nexusUrl> <skipStaging>true</skipStaging> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
解决方案
一、修复测试数据库连接报错
你的Mockito测试未完全隔离数据库依赖,导致启动Spring上下文时尝试连接真实数据库。按以下方式调整:
- Mock数据库相关Bean:在测试类中用
@MockBean替换JPA Repository或DataSource,彻底避免真实数据库连接:@SpringBootTest public class ExampleTest { @MockBean private YourRepository yourRepository; // 替换为你的Repository类 @Test public void testMethod() { // Mock Repository方法的返回值 when(yourRepository.findById(anyLong())).thenReturn(Optional.of(new YourEntity())); // 执行测试逻辑 } } - 调整Jenkins阶段顺序:如果需要跳过测试打包,确保先执行完成测试(生成覆盖率报告),再执行打包命令:
# 先执行测试,生成Jacoco报告 sh 'mvn clean test -P test-coverage' # 再打包,跳过测试 sh 'mvn package -Dmaven.test.skip=true'
二、修复SonarQube覆盖率0%问题
当前配置存在路径不匹配、执行顺序错误的问题,调整如下:
统一Jacoco报告路径:
pom.xml中sonar.jacoco.reportPath改为${project.basedir}/target/jacoco.exec,同时Jenkins脚本中移除重复的-Dsonar.jacoco.reportPath参数,或统一传正确路径:-Dsonar.jacoco.reportPaths=${WORKSPACE}/target/jacoco.exec注意:新版本SonarQube推荐用复数形式
reportPaths而非reportPath。修正Sonar二进制文件路径:
将-Dsonar.java.binaries=.改为-Dsonar.java.binaries=target/classes,Sonar需要找到编译后的class文件才能计算覆盖率。调整Jenkins执行顺序:
必须先执行测试生成Jacoco报告,再执行Sonar扫描,否则Sonar找不到覆盖率数据:# 第一步:清理、执行测试、生成Jacoco报告 sh 'mvn clean test jacoco:report -P test-coverage' # 第二步:Sonar扫描 sh 'mvn sonar:sonar -Dsonar.sources=src/main/java -Dsonar.java.binaries=target/classes -Dsonar.host.url=http://192.168.2.2:9000/ -Dsonar.login=admin -Dsonar.password=sonar'优化pom.xml配置:
- 移除
dependencies中的jacoco-maven-plugin依赖(插件无需放在依赖中,build/plugins里的配置已足够) - 确保Jacoco的
prepare-agent执行在test阶段(当前配置正确,无需修改)
- 移除
内容的提问来源于stack exchange,提问作者Compte Gmail
相关产品推荐
相关产品推荐

