junit和cobertura在spring框架下进行单元测试和生成报告
在前面两篇文章中分别讲解了junit 和cobertura的集成应用,以及如何在spring环境下进行单元测试,现在如何将cobertura集成到spirng环境中,其实很简单,无非在执行cobertura的ant脚本时,将spring加载用到的配置文件复制到cobertura编译的class文件中,配置如下:
?<?xml version="1.0" encoding="UTF-8"?>
<project name="cobertura.examples.basic" default="coverage" basedir=".">
?<description>
???? Cobertura - http://cobertura.sourceforge.net/
???? Copyright (C) 2003 jcoverage ltd.
???? Copyright (C) 2005 Mark Doliner <thekingant@users.sourceforge.net>
???? Cobertura is licensed under the GNU General Public License
???? Cobertura comes with ABSOLUTELY NO WARRANTY
??? </description>
?<property file="build.properties" />
?
?<!--it need all the lib of project-->
?<property name="project.lib.dir" location="../WebContent/WEB-INF/lib" />
?
?<!-- cobertura lib path -->
?<path id="cobertura.classpath">
??<fileset dir="${cobertura.dir}">
???<include name="lib/*.jar" />
??</fileset>
?</path>
?
?<!-- project lib path -->
?<path id="project.classpath">
??<fileset dir="${project.lib.dir}">
???<include name="*.jar" />
??</fileset>
?</path>
?<taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>
?<!--Clean task-->
?<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" />
??<delete file="cobertura.ser" />
?</target>
?<!--int task-->
?<target name="init" description="create dirs of the report and copy the config files which needed by the project.">
??<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}" />
??<!--copy all the config files to the compile path-->
??<copy includeemptydirs="false" todir="${classes.dir}">
??????????? <fileset dir="../src/main/resources">
??????????????? <exclude name="**/*.launch"/>
??????????????? <exclude name="**/*.java"/>
??????????????? <exclude name="**/*.java"/>
??????????? </fileset>
??</copy>
?</target>
?<!--compile task-->
?<target name="compile" depends="init">
??<javac encoding="UTF-8" srcdir="${src.dir}" destdir="${classes.dir}" debug="true">
???<classpath>
????<path refid="project.classpath"/>
????<path refid="cobertura.classpath"/>
???</classpath>
??</javac>
??<javac srcdir="${test.dir}" destdir="${classes.dir}" debug="true">
???<classpath>
????<path refid="project.classpath"/>
????<path refid="cobertura.classpath"/>
???</classpath>
??</javac>
?</target>
?<target name="instrument" depends="init,compile">
??<!--
???Remove the coverage data file and any old instrumentation.
??-->
??<delete file="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="com/morningstar/planning/**/*.class" />
????<exclude name="com/morningstar/planning/**/I*.class" />
????<exclude name="com/morningstar/planning/**/*Test.class" />
????<exclude name="com/morningstar/planning/dao/support/SimpleDaoSupport.class" />
????<exclude name="com/morningstar/planning/base/AbstractTestCase.class" />
???</fileset>
??</cobertura-instrument>
?</target>
?<target name="test" depends="init,compile">
??<junit fork="yes" dir="${basedir}" 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="project.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="${test.dir}">
?????<!--
??????Instrument all the test classes, but
??????don't instrument the application classes.
??????*******************************************
?????-->
?????<include name="com/morningstar/planning/**/*Test.java" />
?????<exclude name="com/morningstar/planning/dao/RecordTest.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-check">
??<cobertura-check branchrate="34" totallinerate="100" />
?</target>
?<target name="coverage-report">
??<!--
???Generate an XML file containing the coverage data using
???the "srcdir" attribute.
??-->
??<cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />
?</target>
?<target name="alternate-coverage-report">
??<!--
???Generate a series of HTML files containing the coverage
???data in a user-readable form using nested source filesets.
??-->
??<cobertura-report destdir="${coverage.html.dir}">
???<fileset dir="${src.dir}">
????<include name="**/*.java"/>
???</fileset>
??</cobertura-report>
?</target>
?
?<target name="coverage" depends="clean,init,compile,instrument,test,coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>
</project>