+-
java – jacoco代码覆盖率报告生成器显示错误:“包中的类”代码覆盖率报告’与执行数据不匹配“
我正在使用jacoco:report标签生成jacoco报告.
我收到的错误如下:

[jacoco:report] Classes in bundle 'Code Coverage Report' do no match with execution data. For report generation the same class files must be used as at runtime.
[jacoco:report] Execution data for class xxxxx does not match.
[jacoco:report] Execution data for class yyyyy does not match.

蚂蚁报告目标如下:

<target name="report">
                <jacoco:report>
                        <executiondata>
                                <file file="${jacocoexec.dir}/${jacocoexec.filename}"/>
                        </executiondata>
                        <!-- the class files and optional source files ... -->
                        <structure name="Code Coverage Report">
                                <classfiles>
                                        <fileset file="./jar/abc.jar"/>
                                </classfiles>
                                <sourcefiles>
                                      <fileset dir="./code/src"/>
                                </sourcefiles>
                        </structure>
                        <!-- to produce reports in different formats. -->
                        <html destdir="${jacoco.report.dir}"/>
                </jacoco:report>
        </target>

这样生成的abc.jar只能使用./code/src.那么为什么会出现这样的错误呢.任何想法?

最佳答案
您收到与classID相关的错误.这是JaCoCo docs-site详细描述的概念. http://www.eclemma.org/jacoco/trunk/doc/classids.html.这是在同一JVM中支持多个版本的类(例如appserver)的关键步骤.

复制部分部分以获得可见性.

什么是类ID以及它们是如何创建的?

Class ids are 64-bit integer values, for example 0x638e104737889183 in
hex notation. Their calculation is considered an implementation detail
of JaCoCo. Currently ids are created with a CRC64 checksum of the raw
class file.

什么可能导致不同的类ID?

Class ids are identical for the exact same class file only
(byte-by-byte). There is a couple of reasons why you might get
different class files. First compiling Java source files will result
in different class files if you use a different tool chain:

>不同的编译器供应商(例如Eclipse与Oracle JDK)
>不同的编译器版本
>不同的编译器设置(例如,调试与非调试)

此外,后处理类文件(混淆,AspectJ等)通常会更改类文件.如果您只是为运行时和分析使用相同的类文件,JaCoCo将运行良好.因此创建这些类文件的工具链无关紧要.

即使文件系统上的类文件相同,JaCoCo运行时代理程序看到的类也可能不同.这通常发生在JaCoCo代理或特殊类加载器预处理类文件之前配置另一个Java代理时.典型的候选人是:

>模拟框架
>应用服务器
>持久性框架

同一页面涵盖了可能的解决方案

处理运行时修改的类有哪些变通方法?

如果在您的设置中在运行时修改了类,则有一些解决方法可以使JaCoCo继续工作:

>如果使用其他Java代理,请确保首先在命令行中指定JaCoCo代理.这样JaCoCo代理应该看到原始的类文件.
>指定JaCoCo代理的classdumpdir选项,并在生成报告时使用转储的类.请注意,只会转储已加载的类,即根本未执行的类将不会显示在报表中,因为未涵盖.
>在运行测试之前使用脱机检测.这样,在进行任何运行时修改之前,类将由JaCoCo检测.请注意,在这种情况下,报告必须使用原始类生成,而不是使用已检测的类生成.

编辑于2017年2月22日

如何使用离线检测:
使用Daniel Atallah提供的以下任务.

//Additional SourceSets can be added to the jacocoOfflineSourceSets as needed by 
project.ext.jacocoOfflineSourceSets = [ 'main' ]
task doJacocoOfflineInstrumentation(dependsOn: [ classes, project.configurations.jacocoAnt ]) {
    inputs.files classes.outputs.files
    File outputDir = new File(project.buildDir, 'instrumentedClasses')
    outputs.dir outputDir
    doFirst {
        project.delete(outputDir)
        ant.taskdef(
            resource: 'org/jacoco/ant/antlib.xml',
            classpath: project.configurations.jacocoAnt.asPath,
            uri: 'jacoco'
        )
        def instrumented = false
        jacocoOfflineSourceSets.each { sourceSetName ->
            if (file(sourceSets[sourceSetName].output.classesDir).exists()) {
                def instrumentedClassedDir = "${outputDir}/${sourceSetName}"
                ant.'jacoco:instrument'(destdir: instrumentedClassedDir) {
                    fileset(dir: sourceSets[sourceSetName].output.classesDir, includes: '**/*.class')
                }
                //Replace the classes dir in the test classpath with the instrumented one
                sourceSets.test.runtimeClasspath -= files(sourceSets[sourceSetName].output.classesDir)
                sourceSets.test.runtimeClasspath += files(instrumentedClassedDir)
                instrumented = true
            }
        }
        if (instrumented) {
            //Disable class verification based on https://github.com/jayway/powermock/issues/375
            test.jvmArgs += '-noverify'
        }
    }
}
test.dependsOn doJacocoOfflineInstrumentation

现在使用“gradlew test jacocoTestReport”命令生成报告.

点击查看更多相关文章

转载注明原文:java – jacoco代码覆盖率报告生成器显示错误:“包中的类”代码覆盖率报告’与执行数据不匹配“ - 乐贴网