1

我对与 Ant 脚本有关的 cobertura 有疑问。我已经阅读了 StackOverflow 上的这两篇文章: SONAR - Measure Code Coverage using Cobertura and Bamboo + sonar.dynamicAnalysis=reuseReports = 0% Rules Compliance

此外,我在互联网上搜索了我的问题的解决方案。现在我希望你能帮助我。

最初的情况:我有一个 Ant 脚本来编译一个库,然后是一个测试目标。现在我添加了 cobertura。一段时间后,我发现类和 ser 文件都已存储。该报告也会生成,但没有覆盖。但现在的问题是 JUnit 测试不起作用。可以在 xml 中找到以下错误消息:

<testcase classname="com.***.***.***.***.***Test" name="test16" time="0.0">
<error message="com/***/***/***/***/***Rule" type="java.lang.NoClassDefFoundError">java.lang.NoClassDefFoundError: com/***/***/***/***/***Rule
at com.***.***.***.***.***Test.setUp(Unknown Source)
at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)

我目前在 Ant 脚本中使用以下代码:

<project name="MainLIB" default="vbuild" basedir="./">
<path id="path.additionaltasks">
    <fileset dir="../BUILD/antlib">
        <include name="cobertura.jar" />
        <include name="log4j-1.2.9.jar" />
        <include name="asm-3.0.jar" />
        <include name="asm-tree-3.0.jar" />
        <include name="jakarta-oro-2.0.8.jar" />
    </fileset>
</path>
<taskdef resource="tasks.properties" classpathref="path.additionaltasks" />

<path id="path.LIB">
    <fileset dir="lib" includes="**/**.jar" />
</path>

<path id="path.UNITTEST">
    <path refid="path.LIB" />
    <pathelement path="build/cobertura/classes" />
    <pathelement path="build/classes" />
    <pathelement path="build/test-classes" />
</path>

<property name="dir.build" value="build" />
<property name="dir.build.classes" value="${dir.build}/classes" />
<property name="dir.build.cobertura" value="${dir.build}/cobertura" />
<property name="dir.build.cobertura.classes" value="${dir.build.cobertura}/classes" />
<property name="dir.build.test.classes" value="${dir.build}/test-classes" />
<property name="dir.build.test.report" value="${dir.build}/test-reports" />
<property name="dir.build.dist" value="${dir.build}/dist" />

<target name="vbuild" depends="build,test" description="Build and run tests" />

<target name="build" description="Compile classes and create .jar file in build/dist">

    <delete dir="${dir.build}" />
    <mkdir dir="${dir.build.classes}" />

    <javac srcdir="src" classpathref="path.LIB" verbose="false" destdir="${dir.build.classes}" includeantruntime="false" includejavaruntime="false" debug="true">
    </javac>

    <mkdir dir="${dir.build.dist}" />

    <jar jarfile="${dir.build.dist}/${jar.name}.jar" basedir="${dir.build.classes}">
    </jar>
</target>

<target name="test" description="Compile and Run jUnit Tests">

    <mkdir dir="${dir.build.test.classes}" />
    <mkdir dir="${dir.build.test.report}" />

    <javac srcdir="test" classpathref="path.UNITTEST" verbose="false" destdir="${dir.build.test.classes}" includeantruntime="false" includejavaruntime="false">
    </javac>

    <delete dir="${dir.build.cobertura}" />
    <mkdir dir="${dir.build.cobertura}" />
    <mkdir dir="${dir.build.cobertura.classes}" />
    <cobertura-instrument todir="${dir.build.cobertura.classes}" datafile="${dir.build.cobertura}/cobertura.ser">
        <fileset dir="${dir.build.classes}" />

    </cobertura-instrument>

    <junit printsummary="yes">
        <classpath>
            <!--<pathelement path="build/cobertura/classes" />-->
            <path refid="path.UNITTEST" />

        </classpath>
        <formatter type="xml" />

        <batchtest todir="${dir.build.test.report}">
            <fileset dir="${dir.build.test.classes}">
                <include name="**/*Test.class" />
            </fileset>
        </batchtest>
    </junit>

    <cobertura-report format="xml" datafile="${dir.build.cobertura}/cobertura.ser" destdir="${dir.build.cobertura}/" />
    <cobertura-report format="html" datafile="${dir.build.cobertura}/cobertura.ser" destdir="${dir.build.cobertura}/" />
</target>

问题是,这些类位于 build/cobertura/classes 目录中。而且我想不通,为什么Junit,找不到课程

4

1 回答 1

0

你的类路径:

    <classpath>
        <!--<pathelement path="build/cobertura/classes" />-->
        <path refid="path.UNITTEST" />

    </classpath>

包含:

<path id="path.LIB">
    <fileset dir="lib" includes="**/**.jar" />
</path>

<path id="path.UNITTEST">
    <path refid="path.LIB" />
    <pathelement path="build/cobertura/classes" />
    <pathelement path="build/classes" />
    <pathelement path="build/test-classes" />
</path>

但它也需要 cobertura 罐子,您在path.additionaltasks. Cobertura 使用字节码操作来检测类文件。这些文件仍然需要 cobertura.jar 依赖项才能工作。
要修复它,请将其添加path.additionaltasks到 junit:

    <classpath>
        <!--<pathelement path="build/cobertura/classes" />-->
        <path refid="path.UNITTEST" />
        <path refid="path.additionaltasks" />
    </classpath>
于 2012-10-10T11:34:49.797 回答