我在做覆盖测试率的时候,用的cobertura ,由于程序本身是用到hibernate  ,所以在测试覆盖测试率的时候发现hibernate 和cobertura的asm.jar版本冲突,我也查过有没有一些解决方法,例如:Why is Cobertura causing me to have classpath conflicts with ASM? 
    Cobertura uses ASM to modify your bytecode. There are a few other popular programs that use ASM; Groovy and Hibernate, to name two. You could have problems if Cobertura uses a different version of asm and you add both versions to your classpath. 
Cobertura only uses ASM when instrumenting. Cobertura does not need ASM in your classpath when running tests. If you're seeing classpath conflicts, just make sure the asm jar that comes with Cobertura is used only by Cobertura, and only when instrumenting.     在build.xml 中 ,确实我在compile的时候可以不用cobertura里面的.jar文件,但是我要做junit测试的时候 ,确实需要cobertura里面的.jar,从下面片断我们可以看出:
   <target name="test" depends="init,compile">
<junit fork="yes" dir="${basedir}" failureProperty="test.failed">

<sysproperty key="net.sourceforge.cobertura.datafile" file="${reports.dir}/basic_coverage.ser" /> <!--
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" /> 
              <formatter type="xml" />
<test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
<batchtest todir="${reports.xml.dir}" unless="testcase">
<fileset dir="${src.dir}">
<include name="**/Test*.java" />
</fileset>
</batchtest>  
</junit> </target>我想问一下又没有什么办法可以解决hibernate 和cobertura的asm.jar冲突问题???, 我想的是先做junit ,然后再做instrument,但是我不知道该如何处理,寻求高手给解决一下!!!!小弟在此谢过了!!!!

解决方案 »

  1.   

    我的build.xml文件是:
    <?xml version="1.0" encoding="UTF-8"?><project name="cobertura" default="coverage" basedir="."><description>Test and coverage . author by Liu,Jifeng</description><property file="build.properties" /><path id="cobertura_classpath">
    <fileset dir="${basedir}">
    <include name="lib/*.jar" />
    </fileset>
    </path><taskdef classpathref="cobertura_classpath" resource="tasks.properties"/> <target name="init">
    <mkdir dir="${classes.dir}" />
    <mkdir dir="${instrumented.dir}" />
    <mkdir dir="${reports.xml.dir}" />
    <mkdir dir="${reports.html.dir}" />
    <mkdir dir="${coverage.xml.dir}" />
    <mkdir dir="${coverage.html.dir}" />
    </target> <target name="compile" depends="init">
    <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes">
    <classpath refid="cobertura_classpath" />
    </javac>
            <copy todir="${classes.dir}">
                <fileset dir="${src.dir}">
                    <exclude name="**/*.java"/>
                </fileset>
            </copy> </target> <target name="instrument" depends="init,compile"> <delete file="${reports.dir}/basic_coverage.ser"/>
    <delete dir="${instrumented.dir}" /> <cobertura-instrument datafile="${reports.dir}/basic_coverage.ser" todir="${instrumented.dir}">
    <ignore regex="org.apache.log4j.*" /> <fileset dir="${classes.dir}">
    <include name="**/*.class" />
    <exclude name="**/Test*.class" />
    </fileset> </cobertura-instrument>
    </target>           
          <target name="test" depends="init,compile">
    <junit fork="yes" dir="${basedir}" failureProperty="test.failed">

    <sysproperty key="net.sourceforge.cobertura.datafile" file="${reports.dir}/basic_coverage.ser" /> <!--
    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" />              <formatter type="xml" />
    <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
    <batchtest todir="${reports.xml.dir}" unless="testcase">
    <fileset dir="${src.dir}">
    <include name="**/Test*.java" />
    </fileset>
    </batchtest>  
    </junit>             <junitreport todir="${reports.xml.dir}">
    <fileset dir="${reports.xml.dir}">
    <include name="TEST-*.xml" />
    </fileset>
    <report format="frames" todir="${reports.html.dir}" />
         </junitreport>
    </target> <target name="coverage-report">
    <cobertura-report datafile="${reports.dir}/basic_coverage.ser" srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />
    <cobertura-report datafile="${reports.dir}/basic_coverage.ser" srcdir="${src.dir}" destdir="${coverage.html.dir}" />
    </target> <target name="clean" description="Remove all files created by the build/test process.">
    <delete dir="${classes.dir}" />
    <delete dir="${instrumented.dir}" />
    <delete dir="${reports.dir}" />
    <delete file="cobertura.log" />
    </target> <target name="coverage" depends="clean,compile,instrument,test,coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/></project>
      

  2.   

    这有同样问题的解决办法http://www.breadtear.cn/post/10.html
      

  3.   

    你的junit任务要加上includeAntRuntime="false">参数,否则,会和ant自带的jar包冲突的。<junit printsummary="on" haltonfailure="no" haltonerror="no" failureproperty="tests.failed" fork="false" includeAntRuntime="false">