This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JUnit Testing

 
Arjun Reddy
Ranch Hand
Posts: 629
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I wrote the following class and a JUNIT test case for it. Could someone tell me if the JUNIT test case I wrote is correct and how it can be improved?

Also, I was wondering, to test if the database insertion has succeeded or not, we can just go and look into the database table right? Why use JUNIT for it? Sorry I�m a beginner in this so please try and bear with me.


Class being tested:


JUNIT test case:


Thanks.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arjun Reddy:

Also, I was wondering, to test if the database insertion has succeeded or not, we can just go and look into the database table right? Why use JUNIT for it?


Repeatability. You can automatically execute the unit test a hundred times a day. If you check the database manually every time, you'll likely get insane...
 
Arjun Reddy
Ranch Hand
Posts: 629
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


Repeatability. You can automatically execute the unit test a hundred times a day. If you check the database manually every time, you'll likely get insane...


Um... Thanks Ilja. The code I wrote though, is it correct? Also, how can I add repeatability in my code? Coz each time I call the insertData() of DatabaseInsertion class from DatabaseInsertionTest class, a new row is inserted. So, do I have to like comment the executeQuery statement in the
DatabaseInsertion class (The class to be tested)?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun,
Your test will only work if the database table starts empty. If this is the case (or can make it the case), the test is fine. If not, you'll want to make the test more specific. I can think of two things:
1) Search for the specific row and make sure you get a match
2) Delete any rows with that search criteria before running the insert (from your test)

Doing both of these will make your test repeatable.
 
Amaru Shakur
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would be hesitant to catch exceptions in your junit test methods. some say you should naturally let them be handled by junit and i agree. because if you get an exception anything done after that before the catch will appear to be a passed test even though you could have alot of bugs hiding due to ending the test from the exceptions you caught. i read a good article about this during the summer that i will try to find.


edit here is the url.

http://blog.platinumsolutions.com/node/113
 
Arjun Reddy
Ranch Hand
Posts: 629
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys but don't you think I should have actually created an object for the class DatabaseInsertion and test the method instead of rewriting all the code again like creating the Connection object etc.. and closing all the connections?/

Thanks.
[ November 12, 2008: Message edited by: Arjun Reddy ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arjun,
Ideally creating a connection is in your codebase just once - either in a framework or library. If that code is tested once, it's fine to refer to it all over.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic