Hi all,
I have a class called Book.java and two
test cases called BookTest1.java and BookTest2.java . I want to test both this test cases by
ant. I have to set this ant task unix cron job so, that this test is done automatically each day.
I have wrote the build file with batchtest option but it is building the application suucessfully without checking the Test Cases individually.
I am successfully the same task with
JUnit Test suite.
Thanks and Regards
Kaushik Banerjee
---------------------------------------------------------------------------
Book.java
---------------------------------------------------------------------------
package src;
public class Book {
private
String title;
private double price;
public Book(String title,double price) {
this.title = title;
this.price = price;
}
public static void main(String args[])
{
System.out.println("Inside Book class");
}
public boolean equals(Object object) {
if (object instanceof Book) {
Book book = (Book) object;
return getTitle().equals(book.getTitle())
&& getPrice() == book.getPrice();
}
return false;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
--------------------------------------------------------------------------
BookTest1.java
--------------------------------------------------------------------------
package src;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
//import de.laliluna.tutorial.junitexample.Book;
/**
* @author
http://www.laliluna.de */
public class BookTest1 extends TestCase {
private Book book1;
private Book book2;
@SuppressWarnings("unused")
private Book book3;
/**
* setUp() method that initializes common objects
*/
protected void setUp() throws Exception {
super.setUp();
book1 = new Book("ES", 12.99);
book2 = new Book("The Gate", 11.99);
book3 = new Book("ES", 12.99);
}
/**
* tearDown() method that cleanup the common objects
*/
protected void tearDown() throws Exception {
super.tearDown();
book1 = null;
book2 = null;
book3 = null;
}
/**
* Constructor for BookTest1.
* @param name
*/
public BookTest1(String name) {
super(name);
}
/**
* testEquals method to test the equals(..) method
* of the class Book
*/
public void testEquals(){
assertFalse(book2.equals(book1));
assertTrue(book1.equals(book3));
}
public static Test suite(){
TestSuite suite = new TestSuite();
suite.addTest(new BookTest1("testEquals"));
return suite;
}
}
---------------------------------------------------------------------------
package src;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
//import de.laliluna.tutorial.junitexample.Book;
public class BookTest2 extends TestCase {
private Book book3;
private Book book4;
private Book book5;
/**
* setUp() method that initializes common objects
*/
protected void setUp() throws Exception {
super.setUp();
book3 = new Book("ES", 12.99);
book4 = new Book("The Gate", 11.99);
book5 = new Book("ES", 12.99);
}
/**
* tearDown() method that cleanup the common objects
*/
protected void tearDown() throws Exception {
super.tearDown();
book3 = null;
book4 = null;
book5 = null;
}
/**
* Constructor for BookTest.
* @param name
*/
public BookTest2(String name) {
super(name);
}
/**
* testEquals method to test the equals(..) method
* of the class Book
*/
public void testEquals(){
assertFalse(book4.equals(book3));
assertTrue(book3.equals(book5));
}
public static Test suite(){
TestSuite suite = new TestSuite();
suite.addTest(new BookTest2("testEquals"));
return suite;
}
}
---------------------------------------------------------------------------
Test Suite(Not neccessary when running with ant)
---------------------------------------------------------------------------
package src;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllJUnitTests {
/*public AllJUnitTests(String name)
{
super(name);
}*/
public static Test suite() {
TestSuite suite = new TestSuite("Test for src");
//$JUnit-BEGIN$
suite.addTest(BookTest2.suite());
suite.addTest(BookTest1.suite());
//$JUnit-END$
return suite;
}
/**
* Runs the test suite using the textual runner.
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
}
---------------------------------------------------------------------------
build.xml
---------------------------------------------------------------------------
<?xml version="1.0"?>
<project default="deploy">
<!-- set global properties for this build -->
<property name="src" value="src" />
<property name="build" value="build" />
<property name="dist" value="dist" />
<target name="JUNIT">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}/src" />
</target>
<target name="compile" depends="JUNIT">
<mkdir dir="${dist}/lib" />
<!-- Compile the
java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}/src" >
<classpath>
<!--<pathelement location="dist/lib/HellowithAnt.jar" />-->
<pathelement path="E:/junit4.4/junit-4.4.jar" />
<!--<pathelement path="/lib/commons-net-1.4.1.jar" />
<pathelement path="/lib/jakarta-oro-2.0.8.jar" />-->
<!--<pathelement path="E:/apache-ant-1.7/lib" />-->
</classpath>
</javac>
<junit printsummary="yes" fork="yes"
errorProperty="batchtest.failed"
failureProperty="batchtest.failed"
haltonfailure="yes">
<formatter type="xml"/>
<classpath path=".">
<pathelement path="E:/junit4.4/junit-4.4.jar" />
<pathelement path="/build/src/BookTest1.class"/>
<pathelement path="/build/src/BookTest2.class"/>
<pathelement path="/build/src/AllJUnitTests.class"/>
</classpath>
<!--<test todir="${build}/src"/>-->
<batchtest todir="${build}/src">
<!--<fileset dir="${build}/src" includes="src.AllJUnitTests.class"/>-->
<fileset dir="${build}/src">
<filename name ="src.BookTest1.class"/>
<filename name ="src.BookTest2.class"/>
<filename name ="src.AllJUnitTests.class"/>
</fileset>
</batchtest>
<!--<batchtest todir="build.src">
<fileset dir="build.src">
<filename name ="src.BookTest1.class"/>
<filename name ="src.BookTest2.class"/>
</fileset>
</batchtest>-->
<!--<test todir="${build}/src" name="src.BookTest"/>-->
</junit>
<fail message="Tests failed!" if="batchtest.failed"/>
<junitreport todir="${build}/src">
<!--<batchtest todir="${build}/src">
<fileset dir="${build}/src">
<include name="*.xml"/>
</fileset>
</batchtest>-->
<report format="frames" todir="${build}/lib"/>
</junitreport>
</target>
<target name="dist" depends="compile">
<!-- Create the ${dist}/lib directory -->
<!-- Put everything in ${build} into the HelloTest.jar file -->
<jar jarfile="${dist}/lib/HellowithAnt.jar" basedir="${build}/src" >
<!--<fileset dir="${dist}/lib">
<filename name="BookTest1.class"/>
<filename name="BookTest2.class"/>
<filename name="Book.class"/>
<filename name="dist.lib.HelloTestwith.jar"/>
</fileset>-->
<manifest>
<attribute name="Main-Class" value="src.Book"/>
</manifest>
</jar>
</target>
<target name="runtests" depends="dist" >
<java jar="${dist}/lib/HellowithAnt.jar"
fork="yes"
classpath=".;D:/Java/jdk1.5.0_01/bin;E:/apache-ant-1.7.0/lib/junit-4.4.jar;C:/Documents and Settings/kaushikb/workspace/HelloTestwithAnt/dist/lib/HellowithAnt.jar;C:/Documents and Settings/kaushikb/workspace/HelloTestwithAnt/src/AllJUnitTests;C:/Documents and Settings/kaushikb/workspace/HelloTestwithAnt/src/BookTest1;C:/Documents and Settings/kaushikb/workspace/HelloTestwithAnt/src/BookTest2">
<!--<fileset todir="${build}/src">
<filename name="src.BookTest1.class"/>
<filename name="src.BookTest2.class"/>
</fileset>-->
<arg value="src.AllJUnitTests"/>
</java>
</target>
<target name="deploy" depends="runtests">
</target>
</project>
---------------------------------------------------------------------------
Command prompt output :
---------------------------------------------------------------------------
c:/Documents and Settings/kaushikb/workspace/HelloTestwithAnt> ant
--------------------------------------------------------------------------
Buildfile: build.xml
JUNIT:
compile:
[javac] Compiling 4 source files to C:\Documents and Settings\kaushikb\wor
pace\HelloTestwithAnt\build\src
[junitreport] Processing C:\Documents and Settings\kaushikb\workspace\HelloTes
ithAnt\build\src\TESTS-TestSuites.xml to C:\DOCUME~1\kaushikb\LOCALS~1\Temp\nu
1759201173
[junitreport] Loading stylesheet jar:file:/E:/apache-ant-1.7.0/lib/ant-junit.j
!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 1153ms
[junitreport] Deleting: C:\DOCUME~1\kaushikb\LOCALS~1\Temp\null1759201173
dist:
[jar] Building jar: C:\Documents and Settings\kaushikb\workspace\HelloTe
withAnt\dist\lib\HellowithAnt.jar
runtests:
[java] Inside Book class
deploy:
BUILD SUCCESSFUL
Total time: 5 seconds
---------------------------------------------------------------------------