You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SonarQube新增测试后代码覆盖率不提升问题排查求助

SonarQube代码覆盖率异常排查问题

问题现象

  • 项目整体代码覆盖率仅9.5%,与代码量不匹配
  • 初始编写的测试未被SonarQube识别,添加jacoco-maven-plugin后新代码覆盖率升至13.2%,但整体覆盖率仍停留在9.5%
  • 后续新增测试已同步至SonarQube平台,但对应代码仍显示无覆盖(例如Categorie.java的测试类CategorieTest.java已在平台可见,但实体类无覆盖记录)

jacoco-maven-plugin配置

<plugin>
 <groupId>org.jacoco</groupId>
 <artifactId>jacoco-maven-plugin</artifactId>
 <version>0.8.8</version>
 <executions>
    <execution>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
    <execution>
      <id>generate-code-coverage-report</id>
      <phase>test</phase>
      <goals>
        <goal>report</goal>
      </goals>
    </execution>
  </executions>
</plugin>

相关代码

Categorie.java(实体类)

package com.back.projetbdi_groupe1.Entities;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class Categorie {
    @Id
    private String idCategorie;

    private String libCateg;

    public String getIdCategorie() {
        return idCategorie;
    }

    public void setIdCategorie(String idCategorie) {
        this.idCategorie = idCategorie;
    }

    public String getLibCateg() {
        return libCateg;
    }

    public void setLibCateg(String libCateg) {
        this.libCateg = libCateg;
    }
}

CategorieTest.java(测试类)

package com.back.projetbdi_groupe1.entities;

import com.back.projetbdi_groupe1.Entities.Categorie;
import org.junit.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CategorieTest {

    @Test
    public void testGetIdCategorie(){
        Categorie categorie = new Categorie();
        categorie.setIdCategorie("1");
        assertEquals("1",categorie.getIdCategorie());
    }

    @Test
    public void testGetLibCateg(){
        Categorie categorie = new Categorie();
        categorie.setLibCateg("categ");
        assertEquals("categ",categorie.getLibCateg());
    }
}

执行提交命令

mvn clean verify sonar:sonar -Dsonar.login=MyFabulousAndLongToken

pom.xml相关配置

Properties节点

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <jettyVersion>9.4.3.v20170317</jettyVersion>
    <jettyServletVersion>9.4.3.v20170317</jettyServletVersion>
    <sonar.host.url>http://im2ag-sonar.u-ga.fr:9000/</sonar.host.url>
    <java.version>17</java.version>
</properties>

测试相关依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>2.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <version>2.5.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

问题排查方向

1. 测试框架版本冲突与混合使用

  • 测试类使用JUnit4的org.junit.Test注解,但断言调用的是JUnit5的org.junit.jupiter.api.Assertions,这种混合写法会导致测试无法被正确执行
  • pom.xml中同时引入JUnit4和JUnit5依赖,Spring Boot 2.4+默认适配JUnit5,冗余的JUnit4依赖会干扰测试执行逻辑
  • 解决:统一使用JUnit5,移除JUnit4依赖,将测试类的@Test替换为org.junit.jupiter.api.Test

2. 包名大小写不一致

  • 实体类包名是com.back.projetbdi_groupe1.Entities(大写E),测试类包名是com.back.projetbdi_groupe1.entities(小写e)
  • 虽然Java对包名大小写不敏感,但Jacoco生成覆盖率报告时,可能因路径大小写不匹配导致无法关联测试与目标类
  • 解决:统一包名大小写,将测试类包名修改为与实体类一致的com.back.projetbdi_groupe1.Entities

3. Java编译版本不匹配

  • pom中maven.compiler.source和target设置为1.8,但java.version是17,编译版本与运行版本不一致,会导致Jacoco无法正确生成覆盖率数据
  • 解决:将maven.compiler.source和target改为17,保持与java.version一致

4. Jacoco报告未明确传递给SonarQube

  • 当前Jacoco配置仅生成本地报告,但未显式指定SonarQube读取报告的路径,可能导致SonarQube无法获取最新覆盖率数据
  • 解决:在pom的properties中添加sonar.jacoco.reportPaths=target/jacoco.exec,确保SonarQube能读取到Jacoco的执行数据

5. 先验证本地测试执行情况

  • 单独执行mvn test,查看控制台输出是否显示CategorieTest的2个测试用例被执行
  • 执行mvn jacoco:report后,打开target/site/jacoco/index.html查看本地覆盖率报告,确认Categorie.java是否有覆盖记录
  • 如果本地报告也无覆盖,优先解决测试框架或包名问题

内容的提问来源于stack exchange,提问作者justin goudon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 03:15:43