aspose file tools*
The moose likes Java in General and the fly likes JUnit and DAO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "JUnit and DAO" Watch "JUnit and DAO" New topic
Author

JUnit and DAO

Sahil Sharma
Ranch Hand

Joined: Aug 27, 2003
Posts: 152
Hi All,

I have written a DAO, which contains four methods (insert,update,retrieve and delete). Now I want to test them using JUnit.But I have never worked on JUnit.
Can someone help me out by telling me on how to test those methods thru JUnit.

Thnx
Sahil
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
please check out this link first by all means
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
http://www.junit.org is probably to come even before that


42
Sahil Sharma
Ranch Hand

Joined: Aug 27, 2003
Posts: 152
import java.sql.ResultSet;

import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/*
* Created on Dec 29, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/

/**
* @author vkumar
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class DAOTestClient extends TestCase{

public static void main(String[] args) {

junit.textui.TestRunner.run (suite());


}

public static Test suite() {
return new TestSuite(DAOTestClient.class);
}

/**
* Method to test the Inert Record method of the DAO
*
*
*/
public void testInsertRecords()
{
ResultSet rs = null;
try{

TitleVO titleVO = new TitleVO(2,0,0,0.0,"I am Ready","Bryan Adams","test","test","test","test","12/28/2004","12/28/2004");
AndersonOTPDAO titleDAO = AndersonOTPDAOFactory.getAndersonOTPDAO("TitleDAOImpl");

boolean bResult = titleDAO.insertRecord(titleVO);
System.out.println("Insert Result "+bResult);
//assertTrue(bResult);

}catch(Exception excp){
throw new AssertionFailedError() ;

}

}

/**
* Method to test the updateRecord() method of the DAO
*
*
*/
public void testUpdateRecords()
{
ResultSet rs = null;
try{

TitleVO titleVO = new TitleVO(2,0,0.0,0.0,"Heaven","Bryan Adams","test","test","test","test","12/28/2004","12/28/2004");
AndersonOTPDAO titleDAO = AndersonOTPDAOFactory.getAndersonOTPDAO("TitleDAOImpl");

boolean bResult = titleDAO.updateRecord(titleVO);
System.out.println("Update Result "+bResult);
//assertTrue(bResult);

}catch(Exception excp){
throw new AssertionFailedError() ;

}

}


/**
* Method to test the retrieveRecords() method of the DAO
*
*
*/
public void testRetrieveRecords()
{
ResultSet rs = null;
try{

AndersonOTPDAO titleDAO = AndersonOTPDAOFactory.getAndersonOTPDAO("TitleDAOImpl");

rs = titleDAO.retrieveRecords(2);

System.out.println("Value of Resultset =>"+rs);
if(rs!=null)
{
while(rs.next())
{
System.out.println("Title ID :: "+rs.getInt("title_id"));
System.out.println("Title:: "+rs.getString("title"));
System.out.println("Artist:: "+rs.getString("artist"));
}
}

}catch(Exception excp){
throw new AssertionFailedError() ;

}

}


/**
* Method to test the deleteRecord() method of the DAO
*
*
*/
public void testDeleteRecords()
{
ResultSet rs = null;
try{

//TitleVO titleVO = new TitleVO(2,0,0.0,0.0,"Heaven","Bryan Adams","test","test","test","test","12/28/2004","12/28/2004");
AndersonOTPDAO titleDAO = AndersonOTPDAOFactory.getAndersonOTPDAO("TitleDAOImpl");

boolean bResult = titleDAO.deleteRecord(2);
System.out.println("Delete Result "+bResult);
//assertTrue(bResult);

}catch(Exception excp){
throw new AssertionFailedError() ;

}

}

}
Sahil Sharma
Ranch Hand

Joined: Aug 27, 2003
Posts: 152
above displayed code I have written.
Is it correct ?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Most people write JUnit tests with few or no displays. When you run a bunch together it's clean to just let JUnit display dots as they run.

The assert statements that you commented out are key. At the end of the run JUnit reports how many passed or failed based on those asserts.

You can remove a ton of try-catch code and just let exceptions fly. Declare your test methods "throw Exception" and let JUnit report the exceptions.

If you expect an exception, you can do this:

Testing with a real database is a bit of a pain, but it is your whole point for this thing. You have to assure that the database is in a known state before you begin. You can use the setUp() method to delete all rows or certain rows before you start, and maybe use tearDown() to delete them again so you don't leave test stuff behind.

Any of that sound good?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Sahil Sharma
Ranch Hand

Joined: Aug 27, 2003
Posts: 152
Does JUnit generate any reports (HTML,Text) of the result. If yes, then how can i do it.
I know it can be done thru ANT but i am not using it. Mine is a standalone class.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can run JUnit as a text program and all the output goes to console, so you could redirect that to a file. You can also run a provided Swing front-end that has a neat green bar but I'm not sure about capturing the console output. And I haven't looked at the Ant task at all. It might be something you could modify to capture what you want. I hear about people putting the test results on web pages all the time, so it's gotta be doable. The Ant task sounds like the most promising place to look.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JUnit and DAO