wood burning stoves 2.0*
The moose likes Testing and the fly likes JUnit Testing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit Testing" Watch "JUnit Testing" New topic
Author

JUnit Testing

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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.


Be Humble... Be Nice.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Amaru Shakur
Ranch Hand

Joined: Jul 17, 2008
Posts: 50
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


Even though im marked for death I will spark till i loose my breath
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

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
 
subject: JUnit Testing