-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate and JUnit testing
PostPosted: Fri Mar 12, 2004 11:04 pm 
Beginner
Beginner

Joined: Thu Mar 04, 2004 11:51 am
Posts: 34
I am trying to build a simple framework using JUnit for testing classes that use Hibernate to persist objects.

These tests will typically create a few objects, persist them to the database, read them back, update, etc. At the end of the test, I want all the objects I added out of the database.

My strategy for doing this is to place each object created in a list, and then iterate the list in reverse order in the tearDown() method and delete each object.

This strategy mostly works except for this: the unit tests may themselves want to test deletion, and then verify that the deleted object no longer exists in the database. However, if the test fails, I still want the tearDown() method to attempt to delete the object, even though it may no longer exist.

My first approach was to wrap each deletion in a try-catch block to catch the exceptions that may occur when the object has already been deleted, and just move on down the list.

But then I read "Chapter 20 - Best Practices" which tells me it's an awfully good idea not to treat exceptions as recoverable.

So my question is first whether what I am doing is really unsafe.
My second question is whether there's a method that will delete an object only if it exists, or whether I should just bite the bullet and try to get() the object (not load() which would throw) and only delete it if it's available.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 13, 2004 9:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You could use get() for you purposes but you could also consider using dbUnit as it wil populate the database and can be used to check the state is correct after making changes to the database.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 9:00 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
Hi,

My practice for unit testing is similar to yours. However, instead of using hibernate to remove objects, I just use straight sql to remove the objects. Most of my tables have a field called "update_source", which in the case of a unit test is UT. So at the end of my tests, i just issue a bunch of prepared statements like:
"delete from x where x.update_source='UT'

Works pretty well so far.

James


Top
 Profile  
 
 Post subject: DBunit
PostPosted: Mon Aug 02, 2004 2:09 pm 
Beginner
Beginner

Joined: Tue Jun 22, 2004 3:16 pm
Posts: 35
I am setting up DBunit to test my HIbernate stuff. Should I have JDBC connection object somehow returned from my DAO in order to have DBUnit get the connection?


Top
 Profile  
 
 Post subject: Negative / positive IDs
PostPosted: Sat Aug 07, 2004 7:59 pm 
Newbie

Joined: Sat Aug 07, 2004 5:30 pm
Posts: 5
Location: Munich
Hi,

a crude-but-workable hack I used for a couple of applications was using negative IDs for unit test / temporary data, and positive IDs for my "normal" interactive test data. My Db-Unit Tests have a common super class with a tear-down method that just goes through all tables and deletes all temporary data with ID < 0.

Now, these were non-Hibernate applications, but I'm sure Hibernate can be tweaked to support a similar schema if that's what you want.

HTH =) Phouk


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.