Posted By:
kaushik_banerjee
Posted On:
Thursday, November 22, 2007 06:34 AM
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 Taton Banerjee --------------------------------------------------------------------------- Book.java --------------------------------------------------------------------------- package src; public class Book { priva
More>>
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
Taton 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"?>
errorProperty="batchtest.failed"
failureProperty="batchtest.failed"
haltonfailure="yes">
-->
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">
/*---------------------------------------------------------------------------
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 Settingskaushikbwor
paceHelloTestwithAnt uildsrc
[junitreport] Processing C:Documents and SettingskaushikbworkspaceHelloTes
ithAnt uildsrcTESTS-TestSuites.xml to C:DOCUME~1kaushikbLOCALS~1Temp
u
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~1kaushikbLOCALS~1Temp
ull1759201173
dist:
[jar] Building jar: C:Documents and SettingskaushikbworkspaceHelloTe
withAntdistlibHellowithAnt.jar
runtests:
[java] Inside Book class
deploy:
BUILD SUCCESSFUL
Total time: 5 seconds
---------------------------------------------------------------------------
<<Less