6/25/10

Simple TestCase

Simple junit testcase example.


Source Package ::

package junittestproject;

/**
 *
 * @author toyopet
 */
public class Book {

    private String title;
    private double price;

    /**
     * Constructor
     *
     * @param title
     * @param price
     */
    public Book(String title,
            double price) {
        this.title = title;
        this.price = price;
    }

    public Book() {
    }

    /**
     * Check if an object is an instance of book
     * and the values of title and price are equal
     * then return true, otherwise return false
     */
    public boolean equals(Object object) {
        if (object instanceof Book) {
            Book book = (Book) object;
            return getTitle().equals(book.getTitle()) && getPrice() == book.getPrice();
        }else{
            return false;
        }
        // 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;
    }
}

Test Package::


package junittestproject;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author toyopet
 */
public class BookTest  {

    public BookTest() {
    }
private Book book1;
private Book book2;
private Book book3;
    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

   /* private BookTest(String string) {
        throw new UnsupportedOperationException("Not yet implemented");
    }*/

    @Before
    public void setUp() {
        try {
         //   super.setUp();
            book1 = new Book("ES", 12.99);
      book2 = new Book("The Gate", 11.99);
book3 = new Book("ES", 12.99);
        } catch (Exception ex) {
            Logger.getLogger(BookTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @After
    public void tearDown() {
        try {
        //    super.tearDown();
            book1 = null;
      book2 = null;
book3 = null;
        } catch (Exception ex) {
            Logger.getLogger(BookTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Test of equals method, of class Book.
     */
    @Test
    public  void testEquals() {
        System.out.println("equals");
        Object object = new Object();;
        Book instance = new Book();
        boolean expResult = false;
        boolean result = instance.equals(object);
        //assertEquals(expResult, result);
        assertFalse(book1.equals(book1));
       //assertFalse(book2.equals(book1));
       // assertTrue(book1.equals(book1));
        // TODO review the generated test code and remove the default call to fail.
       // fail("The test case is a prototype.");
    }

    /**
     * Test of getPrice method, of class Book.
     */
    @Test
    public void testGetPrice() {
        System.out.println("getPrice");
        Book instance = null;
        double expResult = 0.0;
        //double result = instance.getPrice();
        //assertEquals(expResult, result, 0.0);
        // TODO review the generated test code and remove the default call to fail.
//        fail("The test case is a prototype.");
    }

    /**
     * Test of setPrice method, of class Book.
     */
    @Test
    public void testSetPrice() {
        System.out.println("setPrice");
        double price = 0.0;
        Book instance = null;
       // instance.setPrice(price);
        // TODO review the generated test code and remove the default call to fail.
      //  fail("The test case is a prototype.");
    }

    /**
     * Test of getTitle method, of class Book.
     */
    @Test
    public void testGetTitle() {
        System.out.println("getTitle");
        Book instance = null;
        String expResult = "";
        //String result = instance.getTitle();
       // assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
      //  fail("The test case is a prototype.");
    }

    /**
     * Test of setTitle method, of class Book.
     */
    @Test
    public void testSetTitle() {
        System.out.println("setTitle");
        String title = "";
        Book instance = null;
      //  instance.setTitle(title);
        // TODO review the generated test code and remove the default call to fail.
      //  fail("The test case is a prototype.");
    }
/*public static Test suite(){
    TestSuite suite=null;
    try{
       suite = new TestSuite();
      suite.addTest(new BookTest("testEquals"));
    }catch(Exception e){
        e.printStackTrace();
    }
      return suite;

}*/
}

No comments:

Post a Comment

Popular Posts