This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JUnit and DAO

 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 581
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please check out this link first by all means
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.junit.org is probably to come even before that
 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
above displayed code I have written.
Is it correct ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic