My build.xml can run junitTest well but when I tried to build with cobertura instrument, JunitTest failed, Why?

<target name="instrument" depends="compile">
        <echo> ====== run target instrument ==== </echo>
        <!--
                            Remove the coverage data file and any old instrumentation.
                    -->
        <delete file="${junit.dir}/cobertura.ser" />
        <delete dir="${instrumented.dir}" />

        <!--
                            Instrument the application classes, writing the
                            instrumented classes into ${build.instrumented.dir}.
                    -->
        <cobertura-instrument todir="${instrumented.dir}">
            <!--
                                    The following line causes instrument to ignore any
                                    source line containing a reference to log4j, for the
                                    purposes of coverage reporting.
                            -->
            <ignore regex="org.apache.log4j.*" />

            <fileset dir="${classes.dir}">
                <!--
                                            Instrument all the application classes, but
                                            don't instrument the test classes.
                                    -->
                <include name="**/*.class" />
                <exclude name="**/Test*.class" />
            </fileset>
        </cobertura-instrument>
    </target>



<target name="test" description="Run JUnit Tests" depends="instrument">
        <echo> ====== run JUnit Tests ==== </echo>
        <junit fork="true" dir="${junit.dir}" printsummary="on" haltonfailure="yes" failureProperty="test.failed">
            <!--
                                    Note the classpath order: instrumented classes are before the
                                    original (uninstrumented) classes.  This is important.
                            -->
            <classpath location="${instrumented.dir}" />
            <classpath location="${classes.dir}" />

            <!--
                                    The instrumented classes reference classes used by the
                                    Cobertura runtime, so Cobertura and its dependencies
                                    must be on your classpath.
                            -->
            <classpath refid="cobertura.classpath" />
            <classpath refid="testclasspath" />


            <formatter type="xml" />

            <batchtest fork="yes" todir="${reports.xml.dir}">
                <fileset dir="${test.src}">
                    <include name="**/Test*.java" />
                </fileset>
            </batchtest>

        </junit>
    </target>

Anything missing?

target "test":
change the depends="compile", it works fine.
as long as depends="instrument", Junit test failed.

It seemed to me that instrument destroyed junit class path, or something....

Recommended Answers

All 5 Replies

The line 41 was the problem and it seemed that instrument is not working 100%

41 <classpath location="${instrumented.dir}" />

How to fix the instrument ?

The ant out put is:
[cobertura-instrument] Aug 15, 2014 3:24:01 PM net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler saveCoverageData
[cobertura-instrument] INFO: Cobertura: Saved information on 51 classes.

BUILD SUCCESSFUL
Total time: 10 seconds

But apprarently it has fault in it.

My environment:
ant version Apache Ant(TM) version 1.9.2 compiled on July 8 2013
cobertura version 2.0.3
java version "1.7.0_11"

OS Linux

I have tried not to instrument part of classes that resulted in failure of my Test cases, and it worked. But question is why those classes could not be instrumented correctly, any bug in cobertura2.0.3?

In all seriousnes, I think you should try to join JUnit mailing and ask there.

These days builds tend to be based around Gradle (where you either use Gradle plugins or can reuse stuff from Maven or even Ant) or use Maven (however interest here is fading)

It is working now with exclude some of the class using jmockit.
however, when run it on windows with the same build.xml it failed and error message:
BUILD FAILED
build_win.xml:15: t
ertura.ant.InstrumentTask cannot be found
using the classloader AntClassLoader[C:...\
ertura-2.0.3-sources.jar]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.