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

问题描述 投票:2回答:2

我正在使用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.

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

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

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

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

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

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

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

这是我目前的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>

问题:1。基于jdk 1.8.0_101和jdk 1.8.0_111的编译器生成的字节码是否有任何差异?增量更新可以更改字节码吗?或者在主要版本更新期间差异是否显着?

2.为什么即使在实施离线检测后我仍然会收到此错误?我错过了代码中的任何声明吗?我试图保持代码的格式与jacoco文档here中提供的示例类似。

  1. 我还注意到,当两个类包含相同的类路径时,被检测的类的数量(614)与添加到测试包(566)的类的数量不同。这有什么后果吗?
java ant bytecode interpreter jacoco
2个回答
4
投票

基于jdk 1.8.0_101和jdk 1.8.0_111的编译器生成的字节码是否会有任何差异?增量更新可以更改字节码吗?或者在主要版本更新期间差异是否显着?

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

为什么即使在实施离线检测之后我仍然会收到此错误?我错过了代码中的任何声明吗?

消息本身已经在你提到的问题中得到了完美的解释:jacoco code coverage report generator showing error : "Classes in bundle 'Code Coverage Report' do no match with execution data"

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

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

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


0
投票

2.为什么即使在实施离线检测后我仍然会收到此错误?我错过了代码中的任何声明吗?我试图保持代码的格式与jacoco文档中提供的示例类似。

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

例如。在下面的情况中,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);
  }

}
© www.soinside.com 2019 - 2024. All rights reserved.