• Post Reply Bookmark Topic Watch Topic
  • New Topic

Junit in Queries  RSS feed

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI..,

please forgive me if this is a dump question.
I just started learning Junit and imlepted one junit test case.
my doubt is can we write a junit test case for the method which is inserting some data into database.

method :

public void adddata()
{
preparedStatement = con.prepareStatement(addInfo);
preparedStatement.setLong(1, serviceId);
preparedStatement.setInt(2, GroupId);
preparedStatement.executeUpdate();
}


how can we write a test case for this type of senario.

Thnks
KIm
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While you could do that, I suggest to look into one of the numerous JUnit extensions that help ease writing DB-related tests. Some of these are dbunit, sqlunit and jdbcunit.
 
author & internet detective
Marshal
Posts: 37698
575
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kim,
Yes, you can write an integration test using JUnit to test the query.

Here's an outline of common steps:

set up - call tear down (if a prior test failed, you still want this one to work)
the test - call code trying to test and then run a select against the database to confirm it got added properly
tear down - delete record (so can test can run again in future)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!