dependencies { // JUnit 5 testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.9.0' testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.9.0' testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.9.0' // AssertJ testImplementation group: 'org.assertj', name: 'assertj-core', version: '3.19.0' // Mockito testImplementation 'org.mockito:mockito-core:4.11.0' testImplementation 'org.mockito:mockito-junit-jupiter:4.11.0' // Spring Boot Test testImplementation 'org.springframework.boot:spring-boot-starter-test' // Lombok(测试代码也需要) testCompileOnly 'org.projectlombok:lombok:1.18.26' testAnnotationProcessor 'org.projectlombok:lombok:1.18.26' // YAML 测试数据支持(复用主依赖的版本) testImplementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.15.2' testImplementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.2' } test { useJUnitPlatform() finalizedBy jacocoTestReport // 只输出失败的测试信息 testLogging { // 显示所有事件(passed, failed, skipped, standard_out, standard_error) events "passed", "failed", "skipped", "standard_out", "standard_error" // 完整的异常信息 exceptionFormat "full" // 显示异常但不显示完整堆栈 showExceptions true showCauses true showStackTraces true // 显示堆栈跟踪以便调试 showStandardStreams = true // ✅ 启用标准输出/错误流(显示日志) // 在失败时显示详细信息 afterSuite { desc, result -> if (!desc.parent && result.failedTestCount > 0) { println "\n❌ 失败测试数: ${result.failedTestCount}/${result.testCount}" } } } } jacocoTestReport { dependsOn test reports { xml.enabled true csv.enabled false html.enabled true html.destination file("${buildDir}/jacocoHtml") } // 排除不需要测试的类 afterEvaluate { classDirectories.setFrom(files(classDirectories.files.collect { fileTree(dir: it, exclude: [ '**/JunboApplication.class', '**/config/**', '**/entity/**', '**/po/**', '**/bo/**' ]) })) } } // 配置覆盖率阈值 jacocoTestCoverageVerification { dependsOn jacocoTestReport violationRules { rule { limit { minimum = 0.60 // 最低60%覆盖率 } } rule { element = 'CLASS' limit { minimum = 0.50 // 单个类最低50% } excludes = [ '*.config.*', '*.entity.*', '*.po.*', '*.bo.*' ] } } } // 可选:构建时检查覆盖率 // check.dependsOn jacocoTestCoverageVerification