1

I have a question about cobertura in a connection with Ant scripts. I have already read these two articles on StackOverflow: SONAR - Measure Code Coverage using Cobertura and Bamboo + sonar.dynamicAnalysis=reuseReports = 0% Rules Compliance

Furthermore, I have searched in the internet for a solution for my problem. Now I hope you can help me.

The initial situation: I have an Ant script to compile a library and then a test target. Now I have added cobertura. After some time I got it out that the classes and also the ser file is stored. The report is also generated, but with no coverage. But now the problem is that the JUnit test will not work. The following error message can be found in the 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)

I currently use the following code in the Ant script:

<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>

The problem is, the classes are in the build/cobertura/classes directory. And I can't figure out, why Junit, can't find the classes

Community
  • 1
  • 1
user1734614
  • 53
  • 2
  • 4
  • the classpath in junit is missing the cobertura lib files. Those are needed for executing instrumented tests. But your error seems to indicate that your classpath is missing something more. – oers Oct 10 '12 at 11:29

1 Answers1

0

Your classpath:

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

    </classpath>

Contains:

<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>

But it also needs the cobertura jars, which you reference in path.additionaltasks. Cobertura instruments the class files with byte code manipluation. Those files still need the cobertura.jar dependency to work.
To fix it add the path.additionaltasks to junit:

    <classpath>
        <!--<pathelement path="build/cobertura/classes" />-->
        <path refid="path.UNITTEST" />
        <path refid="path.additionaltasks" />
    </classpath>
oers
  • 18,436
  • 13
  • 66
  • 75