java 类的执行数据不匹配 + Jacoco

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44776226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:20:17  来源:igfitidea点击:

Execution data for class does not match + Jacoco

javaantbytecodeinterpreterjacoco

提问by xmacz

I'm using Jacocoto find the code coverage of unit tests with ANTbut the report isn't generated and I get this error sequence:

我正在使用Jacoco查找带有ANT的单元测试的代码覆盖率,但未生成报告并且我收到以下错误序列:

[jacoco:report] Loading execution data file C:\JUnit\apache-ant-1.10.1\jacoco.ex
ec

[jacoco:report] Writing bundle 'Test' with 566 classes

[jacoco:report] Classes in bundle 'Test' 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 com/!!/AccountDetails does not match.

[jacoco:report] Execution data for class com/!!/DataExtractorHelper does not match.

[jacoco:report] Execution data for class com/!!/WelcomeLetter does not match.

[jacoco:report] Execution data for class com/!!/WelcomeLetterABCD does not match.

I've read these answers but none seemed to help me solve the problem.

我已经阅读了这些答案,但似乎没有一个能帮助我解决问题。

jacoco code coverage report generator showing error : "Classes in bundle 'Code Coverage Report' do no match with execution data"

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

jacoco: For report generation the same class files must be used as at runtime

jacoco:对于报告生成,必须使用与运行时相同的类文件

I compiled all classes on Eclipse and used the ANT build tool to perform code coverage on these classes. I do make use of some external jars due to some dependencies and they've been compiled on jdk 1.8.0_101 and I'm using jdk 1.8.0_111 (I tried solving this error by using jdk 1.8.0_101 but I got the same errors)

我在 Eclipse 上编译了所有类,并使用 ANT 构建工具对这些类执行代码覆盖。由于某些依赖性,我确实使用了一些外部 jar,并且它们已在 jdk 1.8.0_101 上编译,我使用的是 jdk 1.8.0_111(我尝试使用 jdk 1.8.0_101 解决此错误,但我遇到了相同的错误)

It has been mentioned that the class ID might change in Eclipse vs Oracle JDK compilation. Therefore I also checked this case by compiling some basic classes on Eclipse and used the jdk + ANT to find the code coverage. It worked in this case. There is no compilation taking place in the code coverage task. The .class files just need to be checked for coverage.

已经提到类 ID 可能会在 Eclipse 与 Oracle JDK 编译中发生变化。因此我也通过在Eclipse上编译一些基本类来检查这个案例,并使用jdk + ANT查找代码覆盖率。在这种情况下它起作用了。在代码覆盖任务中没有进行编译。.class 文件只需要检查覆盖范围。

All the classes mentioned in the errors have been compiled on eclipse before testing for the code coverage.

在测试代​​码覆盖率之前,错误中提到的所有类都已在 Eclipse 上编译。

I've tried using the offline instrumentationtool as a workaround to the persistence framework being used but it still gives me these errors. All the classes mentioned above in the errors are present in the Instrumented classes folder.${dest.dir}

我已经尝试使用离线检测工具作为正在使用的持久性框架的解决方法,但它仍然给我这些错误。 上面在错误中提到的所有类都存在于 Instrumented classes 文件夹中。${dest.dir}

This is my build.xml at the moment.

这是我目前的 build.xml。

<target name="instrument">
    <delete dir="${dest.dir}"/>
    <mkdir dir="${dest.dir}"/>
    <jacoco:instrument destdir="${dest.dir}">
        <fileset file="D:/NEON/HW/!!/module/!!/bin" includes="**/*.class"/>
        <fileset file="D:/NEON/HW/!!/testprojects/!!/bin" includes="**/*.class"/>
    </jacoco:instrument>
</target>


<target name="cov-test" depends="instrument">
    <mkdir dir="${report.dir}"/>    
    <jacoco:coverage>
        <junit fork="true" forkmode="once" showoutput="true" printsummary="on" enabletestlistenerevents="true">

            <classpath>
                <path refid="ALL.jars"/>
                <path refid="classpath"/>
                <pathelement location="C:/JUnit/jacoco-0.7.9/lib/jacocoagent.jar"/>
                <pathelement location="C:/JUnit/JARS/!!/config/"/>
                <pathelement path="C:/JUnit/apache-ant-1.10.1/InstrClasses"/>
            </classpath>

            <sysproperty key="jacoco-agent.destfile" file="jacoco.exec"/>



            <test name="Fully qualified classname"/>

            <formatter type="plain"/>
            <formatter type="plain" usefile="false" />
            <batchtest fork="yes" todir="${report.dir}">
                <fileset dir="${src.dir}" includes="Fully qualified classname.java"/>
            </batchtest>
        </junit>
    </jacoco:coverage>
</target>


<target name="cov-report" depends="cov-test">
    <jacoco:report>
        <executiondata>
            <file file="jacoco.exec" />
        </executiondata>
        <structure name="Test">
            <classfiles>
                <fileset dir="D:/NEON/HW/!!/module/!!/bin"/>
                <fileset dir="D:/NEON/HW/!!/testprojects/!!/bin"/>
            </classfiles>
            <sourcefiles>
                <fileset dir="D:/NEON/HW/!!/module/!!/src"/>
                <fileset dir="D:/NEON/HW/!!/testprojects/!!/src"/>
            </sourcefiles>      
        </structure>
        <csv destfile="${report.dir}/report.csv" />
    </jacoco:report>

</target>

Questions:1.Will there be any difference in the bytecode generated by compilers based on jdk 1.8.0_101 and jdk 1.8.0_111? Can incremental updates change bytecode? Or is the difference only significant during major version updates?

问题:1.基于jdk 1.8.0_101和jdk 1.8.0_111的编译器生成的字节码会有区别吗?增量更新可以改变字节码吗?还是仅在主要版本更新期间差异才显着?

2.Why am I still getting this error even after offline instrumentation has been implemented? Am I missing out on any declaration in the code? I've tried to keep the format of the code as similar to that of the example provided in the jacoco documentation here.

2.为什么在实现离线检测后我仍然收到这个错误?我是否遗漏了代码中的任何声明?我试图使代码的格式与此处jacoco 文档中提供的示例的格式相似。

  1. I've also noticed that the number of classes being instrumented(614) differs from the number of classes being added to the Test bundle (566) when both contain the same classpaths. Could that be of any consequence?
  1. 我还注意到,当两者都包含相同的类路径时,被检测的类的数量(614)与添加到测试包的类的数量(566)不同。那会有什么后果吗?

回答by Godin

Will there be any difference in the bytecode generated by compilers based on jdk 1.8.0_101 and jdk 1.8.0_111? Can incremental updates change bytecode? Or is the difference only significant during major version updates?

基于jdk 1.8.0_101和jdk 1.8.0_111的编译器生成的字节码会有区别吗?增量更新可以改变字节码吗?还是仅在主要版本更新期间差异才显着?

Yes - in general any different versions (without exceptions) of compilers can generate different bits of bytecode.

是的 - 通常任何不同版本(无一例外)的编译器都可以生成不同位的字节码。

Why am I still getting this error even after offline instrumentation has been implemented? Am I missing out on any declaration in the code?

为什么即使在实施离线检测后我仍然收到此错误?我是否遗漏了代码中的任何声明?

The message itself is already perfectly explained in SO question to which you refer: jacoco code coverage report generator showing error : "Classes in bundle 'Code Coverage Report' do no match with execution data"

消息本身已经在您参考的 SO 问题中得到了完美的解释:jacoco 代码覆盖率报告生成器显示错误:“捆绑包‘代码覆盖率报告’中的类与执行数据不匹配”

More precise answers require a Minimal, Complete, and Verifiableexample (https://stackoverflow.com/help/mcve) to be provided by you, and unfortunately in my opinion just excerpt of build.xmlwith some comments is not Completeand Verifiableexample. And not clear which role Eclipse is playing here in addition to JDK. By the way Eclipse has and uses its own Eclipse Java Compiler.

更精确的答案需要您提供最小、完整可验证的示例 ( https://stackoverflow.com/help/mcve),不幸的是,在我看来,仅摘录build.xml一些评论并不是完整可验证的示例。并且不清楚 Eclipse 除了 JDK 之外还扮演什么角色。顺便说一下,Eclipse 拥有并使用它自己的 Eclipse Java 编译器。

I've also noticed that the number of classes being instrumented(614) differs from the number of classes being added to the Test bundle (566) when both contain the same classpaths. Could that be of any consequence?

我还注意到,当两者都包含相同的类路径时,被检测的类的数量(614)与添加到测试包的类的数量(566)不同。那会有什么后果吗?

Yes - consequence is that what was instrumented is not the same as what was analyzed for generation of report. Which also correlates with messages about mismatch.

是的 - 结果是所检测的内容与为生成报告而分析的内容不同。这也与有关不匹配的消息相关。

回答by Kashyap

2.Why am I still getting this error even after offline instrumentation has been implemented? Am I missing out on any declaration in the code? I've tried to keep the format of the code as similar to that of the example provided in the jacoco documentation here.

2.为什么在实现离线检测后我仍然收到这个错误?我是否遗漏了代码中的任何声明?我试图使代码的格式与此处 jacoco 文档中提供的示例的格式相似。

I've noticed this when the class under test is added to PrepareForTest annotationin it's test class.

当被测类被添加到它的测试类中的PrepareForTest 注释时,我注意到了这一点。

E.g. in following situation, Foo.java will have 0 coverage with error Execution data for class Foo does not match..

例如,在以下情况下, Foo.java 将有 0 个错误覆盖Execution data for class Foo does not match.

@PrepareForTest({ Foo.class })
public class FooTest {

  @Test
  public void test1() {
    Foo f = new Foo();
    String result = f.doSomething();
    assertEquals(expectedResult, result);
  }

}