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

Gradle项目构建报错:Jacoco代码行覆盖率未达最低要求求助

问题描述

我正在学习编写测试用例以理解相关机制,构建Gradle项目时碰到如下错误:

Rule violated for bundle java-gradle-examples: lines covered ratio is 0.0, but expected minimum is 0.5

我的Gradle配置(build.gradle)

plugins {
    id 'java'
    id 'jacoco'
}

group = 'pl.something'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '16'

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.6'
}

dependencies {
    compileOnly 'org.projectlombok:lombok:1.18.24'
    annotationProcessor 'org.projectlombok:lombok:1.18.24'

    testCompileOnly 'org.projectlombok:lombok:1.18.24'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'

    testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version:'5.0.0-M4'
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}
test {
    useJUnitPlatform()
    testLogging {
        events "skipped", "failed"
    }
    finalizedBy jacocoTestReport
}
jacoco {
    toolVersion = "0.8.7"
}
jacocoTestReport {
    dependsOn test
    reports {
        xml.required = false
        csv.required = false
        html.outputLocation = layout.buildDirectory.dir('jacocoReports')
    }
}
jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                counter = 'LINE'
                value = 'COVEREDRATIO'
                minimum = 0.5
            }
        }
    }
}
check.dependsOn jacocoTestCoverageVerification

测试类代码

@Test
void add() {
    int left = 2;
    int right = 1;
    int expected = 3;

    Integer result = left + right;
    Assertions.assertEquals(expected, result);
}

@Test
void subtract() {
    int left = 2;
    int right = 1;
    int expected = 1;

    Integer result = left - right;
    Assertions.assertEquals(expected, result);
}

@Test
void multiply() {
    int left = 2;
    int right = 1;
    int expected = 2;

    Integer result = left * right;
    Assertions.assertEquals(expected, result);
}

@Test
@Disabled("I disabled this test for example")
void divide() {
    int left = 2;
    int right = 1;
    int expected = 2;

    Integer result = left / right;
    Assertions.assertEquals(expected, result);
}

主类代码

public class Main {

    public static void main(String[] args) {
        System.out.println(Calculator.add(1, 2));
        Calculator calculator = new Calculator();
        Calculator.substract(5, 3);
    }

}

问题原因及解决办法

  1. 测试未覆盖业务代码:你的测试只是直接做数值计算,完全没调用Calculator类的方法,Jacoco检测不到任何业务代码被执行,所以覆盖率为0。
  2. 重复配置test任务:build.gradle里写了两个test块,后面的会覆盖前面的配置,虽然不影响覆盖率,但建议合并。

具体修复步骤:

  • 修改测试类,调用真实业务方法:
    把测试方法改成调用Calculator的对应方法,比如add测试:

    @Test
    void add() {
        int left = 2;
        int right = 1;
        int expected = 3;
    
        Integer result = Calculator.add(left, right); // 调用Calculator的add方法
        Assertions.assertEquals(expected, result);
    }
    

    其他测试方法同理,替换为调用Calculator的subtract(注意主类里拼写是substract,需和测试调用保持一致)、multiply方法。

  • 合并test任务配置:
    将两个test块合并成一个:

    test {
        useJUnitPlatform()
        testLogging {
            events "passed", "skipped", "failed"
        }
        finalizedBy jacocoTestReport
    }
    
  • 校验Calculator类的方法:确保Calculator类存在对应的静态/实例方法,保证测试能正常调用执行。

完成上述修改后,重新运行./gradlew check,Jacoco就能检测到代码覆盖,只要覆盖率达到50%以上就不会再报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 13:36:15