ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – jacoco代码覆盖率报告生成器显示错误:“包中的类”代码覆盖率报告’与执行数据不匹配“

2019-09-28 15:11:52  阅读:2033  来源: 互联网

标签:jacoco java ant


我正在使用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”命令生成报告.

标签:jacoco,java,ant
来源: https://codeday.me/bug/20190928/1827965.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有