JUnit with Netbeans

In my office, TDD is a new methodology that we've just began adopting. My senior wrote a TestCase class before he left the company. Thanks to him, I got some sample codes to study.

After baby sitting, I decided to open my computer and fire up netbeans. We're using RAD at my work btw, I could use eclipse but I like netbeans for learning something new or trying out small things. Oh, that makes me an unofficial netbeans evangelist at the same time! Lolz!

Using the JBible app I created a few weeks ago, I created a simple JUnit test class that tests one of my DAO(Data Access Object) methods.

 

What did I gain?

1. For some reason,  I actually enjoyed doing it.

2. I can now officially perform TDD at my work! Though it may take a while to improve on that. My knowledge of TDD is not so advanced afterall.

3. I don't know if it's right to say, but in netbeans you get instant profiling of your methods(if you look on the lower-left where the test status are).

 

There are probably more complicated scenarios to test against. For now, this will do for me.

Below is the code I used for testing. Please forgive the formatting. Also, if you have comments on things like how I can improve my test code, please don't hesitate to comment. I would also appreciate if you could give me pointers so I could get better at this.

Special thanks to Jop and Cruizer, though I may never become an advocate of TDD as you are guys.


package org.devpinoy.bibleapp.dao.test;

import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.devpinoy.bibleapp.dao.BibleDAO;
import org.devpinoy.bibleapp.dao.DAOHelper;
import org.devpinoy.bibleapp.dao.exceptions.NoBookFoundException;
import org.devpinoy.bibleapp.dao.impl.BibleDAOImpl;
import org.devpinoy.bibleapp.dto.Book;
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.*;

public class BibleDAOTest {

    BibleDAO bibleDAO;
   
    public BibleDAOTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

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

    @Before
    public void setUp() {
        try {
            bibleDAO = new BibleDAOImpl(DAOHelper.getAccessDBConnection("C:/BibleDB/BibleDB.mdb"));
        }
        catch (SQLException ex) {
            Logger.getLogger(BibleDAOTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @After
    public void tearDown() {
        bibleDAO = null;
    }
   
    @Test
    public void bibleDAOTest(){
        assertNotNull("BibleDAO is null, everything else will fail", bibleDAO);
    }
   
    @Test
    public void getBookListTest(){
        List bookList = null;
       
        try {
            bookList = bibleDAO.getBookList();
        }
        catch (NoBookFoundException ex) {
            Logger.getLogger(BibleDAOTest.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        assertNotNull("List should not be null", bookList);
        assertTrue("List should have value greater than zero", bookList.size() > 0);
       
        System.out.println("Book list size: " + bookList.size() );
       
        for (int i = 0; i < bookList.size(); i++){
            Book book = (Book)bookList.get(i);
           
            System.out.println("Book" + i + ": " + book);
           
            assertTrue("Only book instance should exist inside getBookList", book instanceof Book);
            assertNotNull("Book should not be null", book);
        }
       
    }
   
    @Test
    public void testNothing(){
        System.out.println("Testing nothing");
    }

}

 

Published 11-15-2008 7:39 PM by lamia
Filed under: , , ,

Comments

# re: JUnit with Netbeans

Monday, November 17, 2008 1:22 AM by cruizer

good start :) i plan to do the same in my blog here this week (when I find the time)