Tuesday, August 30, 2011

Ant script for JUnit

Ant script is build to test the JUnit test framework. Before creating any test case we need Java compiler, Ant and JUnit.
we need junit.jar in your Ant's library folder or have jar file in our project lib folder.

Sample JUnit Test class to fecth records.

import junit.framework.TestCase;
public class EmpTest extends TestCase {
    /**
     * fetch records based on Employee ID from Employee table.
     */
    public final void testFetchByID() {
        Integer empId = 2;
        EmpTO empTO = empDAO.findById(empId);

        System.out.println("Fetch Employee ID " + empId
                + ", FirstName : " + empTO.getFirstName());
    }
}

Build.xml for JUnit

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="test">

    <!-- Source and class folders used in project-->
    <property name="testdir" location="bin" />
    <property name="srcdir" location="source" />
    <property name="srctestdir" location="source-test" />

    <!-- consists of destination class files and jar files from lib folder-->
    <path id="test.classpath">
        <pathelement location="${testdir}" />
        <fileset dir="${basedir}/lib">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <!-- Delete all the class files from project -->
    <target name="clean">
        <delete>
            <fileset dir="${testdir}" includes="**/*.class" />
        </delete>
    </target>
   
    <!-- compiled source folder of project -->
    <target name="compile" depends="clean">
        <javac srcdir="${srcdir}" destdir="${testdir}">
            <classpath refid="test.classpath" />
        </javac>
        <javac srcdir="${srctestdir}" destdir="${testdir}">
            <classpath refid="test.classpath" />
        </javac>
    </target>
   
    <!-- Testing JUnit code by junit task -->
    <target name="test" depends="compile">   
       
        <junit printsummary="yes" haltonfailure="yes" showoutput="yes">
            <classpath refid="test.classpath" />
           
            <!-- The results of the tests can be printed in different formats.
            Output will always be sent to a file, unless you set the usefile attribute to false.
            The formatter named brief will only print detailed information for testcases that failed -->
           
            <formatter type="brief" usefile="false" />
           
            <!-- Define a number of tests based on pattern matching.
            fork      Run the tests in a separate VM.
            -->
            <batchtest fork="yes">
                <fileset dir="${basedir}/source-test/">
                    <include name="**/*Test.java" />
                </fileset>
            </batchtest>
        </junit>
    </target>
</project>

Output of xml file mostly look like this

Buildfile: E:\workspace\Project\EmployeeRecordProj\build.xml
clean:
compile:
    [javac] Compiling 6 source files to E:\workspace\Project\PROJECT_NAME\bin
test:
    [junit] Running com.test.EmpTest
    [junit] Testsuite: com.test.EmpTest
    [junit] Fetch Employee ID 2, FirstName : Harsha
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 1.61 sec
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 1.61 sec
    [junit] ------------- Standard Output ---------------
    [junit] Fetch Employee ID 2, FirstName : Harsha
    [junit] ------------- ---------------- ---------------
    [junit] ------------- Standard Error -----------------
    [junit] ------------- ---------------- ---------------
BUILD SUCCESSFUL
Total time: 3 seconds

No comments :

Post a Comment