Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences

Edit this page


Referenced by
Articles




JSPWiki v2.2.33

[RSS]


Hide Menu

AppFuseCobertura


Coverage testing with Appfuse and Cobertura

This articel will give a short overview how to integrate Cobertura in AppFuse. I assume that you have a running AppFuse project:

  • [1] Download the latest version of Cobertura
  • [2] Integrate the libraries in AppFuse
  • [3] Integrate new Cobertura ANT-Tasks in the build.xml
  • [4] Define new macros for Junit testing with Cobertura
  • [5] Change test targets for Junit testing with Cobertura
  • [6] Define new targets for generating the coverage reports
  • [7] Test it!

Download the latest version of Cobertura [#1]

  • The latest version of Cobertura is 1.7.

Integrate the libraries in AppFuse [#2]

  • Create a new folder named 'cobertura-1.7' int the /lib directory of your AppFuse project.
  • Copy cobertura.jar and the lib folder of the downloaded cobertura version in the new directory
  • Add the following part to the lib/lib.properties file:

#
# Cobertura - http://cobertura.sourceforge.net
#
cobertura.version=1.7
cobertura.dir=${lib.dir}/cobertura-${cobertura.version}
cobertura.jar=${cobertura.dir}/cobertura.jar
  • Finally you have to add the reference to the cobertura.jar to all test classpaths. In order to do this, you have to edit your properties.xml file and add to every test target the element

So for example the dao.compile.classpath would look like

 <path id="dao.test.classpath">
        <path refid="dao.compile.classpath"/>
        <pathelement location="${dbunit.jar}"/>
        <pathelement location="${junit.jar}"/>
        <pathelement location="${commons-collections.jar}"/>
        <pathelement location="${log4j.jar}"/>
	<pathelement location="${cobertura.jar}"/>
    </path>

Integrate new Cobertura ANT-Tasks in the build.xml [#3]

In order to use cobertura with your junit tests, you have to add the cobertura ANT-tasks to your build.xml
  • Open your build.xml and add the following part the the init target:
<path id="cobertura_classpath">
 <fileset dir="${cobertura.dir}">
   <include name="cobertura.jar" />
 </fileset>
 <fileset dir="${cobertura.dir}/lib">
   <include name="**/*.jar" />
 </fileset>
</path>
<taskdef classpathref="cobertura_classpath" resource="tasks.properties"/>

Define new macros for Junit testing with Cobertura [#4]

Cobertura needs to instrument the code in order to calculate the coverage during the tests. To do this, cobertura provides a macro definition called cobertura-instrument. Copy the following macro in your build.xml:
<macrodef name="instrument">
  <attribute name="module"/>
  <attribute name="additional.src.dirs" default=""/>
  <element name="options" optional="yes"/>
  <sequential>
   <echo level="info">Instrumenting classes...</echo>
	<!--
	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="${build.dir}">
	
           <!--
	   Instrument all the application classes, but
	   don't instrument the test classes.
	   -->
	   <include name="**/*.class" />
	   <exclude name="**/*Test.class" />
	</fileset>
   </cobertura-instrument>
  </sequential>
</macrodef>

Now you have to specify, where cobertura should store the instrumented classes (You have to define the instrumented.dir variable). Add the following line to your build.properties:

instrumented.dir=${build.dir}/instrumtented

Change test targets for Junit testing with Cobertura [#5]

Finally, we have to make sure that our code is instrumented before running the Junit tests and that the Junit test cases use the instrumented code. In order to do this, we need to change the macro test-module.

Add the line to the macrodef. Finally it should look something linke this:

<macrodef name="test-module">
  <attribute name="module"/>
  <attribute name="additional.src.dirs" default=""/>
  <element name="options" optional="yes"/>

  <sequential>
    <echo level="info">Testing @{module}...</echo>
    <mkdir dir="${test.dir}/data"/>
    <propertycopy name="testcase" from="@{module}-testcase" silent="true"/>
    <!-- Replace tokens in test properties files -->
    <copy todir="${test.dir}/@{module}/classes">
       <fileset dir="test/@{module}" excludes="**/*.java"/>
       <filterset refid="variables.to.replace"/>
     </copy>
     <instrument module="@{module}"/> <!-- Thats the new line -->
   ......

Additional we need a sysproperty in order to tell cobertura where to store the coverage results and a new classpath reference to the instrumented code:

....
<junit printsummary="no" forkmode="once" errorProperty="test.failed"
 failureProperty="test.failed" fork="${junit.fork}">
    <sysproperty key="net.sourceforge.cobertura.datafile" file="${basedir}/cobertura.ser" /> <!-- Thats a new line -->
    <classpath location="${instrumented.dir}" /> <!-- Thats a new line -->

...

Define new targets for generating the coverage reports [#6]

Add the following target to your build.xml.
<target name="test-reports-coverage" depends="init">
  <!--
  Generate a series of HTML files containing the coverage
  data in a user-readable form using nested source filesets.
  -->
  <mkdir dir="${test.dir}/coverage"/>
     <cobertura-report destdir="${test.dir}/coverage">
     <fileset dir="${src.dir}/dao">
	<include name="**/*.java"/>
     </fileset>
     <fileset dir="${src.dir}/service">
       <include name="**/*.java"/>
     </fileset>
     <fileset dir="${src.dir}/web">
	<include name="**/*.java"/>
     </fileset>
  </cobertura-report>
</target>

and define in your build.properties, where cobertura can find the sources by adding the line

src.dir=${basedir}/src

Test it! [#7]

Now you can test your tests by running ant test-dao and ant test-reports-coverage.

Like Matt would say: Yeah Baby, Yeah: BUILD SUCCESSFUL



Go to top   Edit this page   More info...   Attach file...
This page last changed on 06-Nov-2006 13:53:00 MST by PeterSchneider-Manzell.