Wednesday 22 June 2011

JunitDoclet



To better understand the reflection api , I was using java.lang.reflect.* for creation of  junit skeleton on the go and later discovered there is a similar stuff which creates the junit structure and the suites for the specified package called JUNITDOCLET.

Following is the build.xml used for creation of junit classes and the suites for the classes under package com.abc.dummy and can be specified as com.abc.*.It takes care of all the lifecycle methods of junit. This JunitDoclet is most handy when the classes have more number of method and it supports 3.x and 4.x versions of junit depending on the Junitdoclet jar in the buildpath ;-)



<?xml version="1.0" encoding="UTF-8"?>
<project name="Dummy" default="junittest" basedir=".">
<description>
simple example build file for JunitDoclet task
    </description>
<!-- set global properties for this build -->
<property name="src" location="src" />
<property name="build" location="build" />
<property name="dist" location="dist" />
<property name="lib.dir" value="lib" />
<property name="junit1" value="./junit1" />
<property name="classes" value="${build}/classes" />
<property name="package" value="com.abc.dummy" />
<property name="testsuite" value="${package}.DummySuite" />
<path id="classpath_default">
<pathelement path="${classes}" />
<!-- the javadoc classes are in this package -->
<pathelement path="${java.home}/../lib/tools.jar" />
<fileset dir="${lib.dir}">
<include name="junit-4.8.1.jar" />
<include name="junitdoclet-1.6.jar" />
</fileset>
</path>
<target name="init">
<!-- Create the time stamp -->
<tstamp />
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}" />
</target>

<target name="compile" depends="init" description="compile the source ">
<echo message="=============Compiling================" />
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}">
<classpath>
<pathelement location="${lib.dir}/" />
<pathelement location="${lib.dir}/junit-4.8.1.jar" />
</classpath>
</javac>
<echo message="=============Compiling Completed================" />
</target>
<target name="junitdoclet" depends="compile">
<echo message="=============JUNIT DOCLET STARTED================" />
<javadoc packagenames="com.abc.dummy" sourcepath="${src}"
defaultexcludes="yes" doclet="com.objectfab.tools.junitdoclet.JUnitDoclet"
docletpathref="classpath_default" additionalparam="-d ${junit1} -buildall">
<classpath refid="classpath_default" />
</javadoc>
<echo message="=============JUNIT DOCLET COMPLETED================" />
<echo message="${testsuite}"></echo>
</target>

<target name="junitcompile" depends="junitdoclet">
<javac srcdir="${junit1}" destdir="${classes}" debug="on">
<classpath refid="classpath_default" />
</javac>
</target>
<echo message="----------------------------------------------"/>
<echo message="${classpath_default}"/>
<echo message="----------------------------------------------"/>
<target name="junittest" depends="junitcompile">
<junit fork="yes" haltonfailure="no">
<formatter type="plain" />
<test name="${testsuite}" outfile="testresults" />

<classpath refid="classpath_default" />
</junit>
</target>
</project>

No comments:

Post a Comment