-->
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.  [ 1 post ] 
Author Message
 Post subject: Unit Testing persistance in hibernate
PostPosted: Thu Apr 15, 2010 12:31 pm 
Newbie

Joined: Thu May 31, 2007 3:27 am
Posts: 11
I would like your ideas about unit testing in hibernate. I'm not thinking about dao mockups or using a in-memory database which I already use anyway.

The recurring design problem that I have when unit testing my domain class is that some class have mandatory dependencies on other classes (e.g. "many-to-one" with "not-null" constraint relationships), so to test persistence on a given object, all its dependencies have to be first persisted correctly which indirectly means that all other dependencies must be already unit-tested (given I'm using a test driven development approach). I always end-up creating lots of hibernate mapping files before being able to unit test the persistence of a single class, so my unit tests look more like integration tests. When a particular domain object unit test fails because of an invalid mapping attribute or any other persistence error, all other unit tests that depends on that domain object basically fail too. So, this kind of breaks the isolation of my unit tests.

What do you do to prevent this situation?

1) You don't unit test the persistence of a domain class, you just test the persistence at the DAOs level?
2) You test top-down, so parent objects are tested before child objects
3) You don't map dependencies that are not mapped yet (you come back to complete your unit test when they are)

I was thinking of an approach where dependencies would be mapped differently in my unit test. Let say that I have a "CustomerBill" class that refers to a "Customer" class. I want to unit test the persistence of the "CustomerBill" without depending on the "real" mapping of the "Customer" class. So my "CustomerBill" unit test could load a configuration where the "Customer" would be simply mapped as a table with an id, with no other fields. This way, the "CustomerBill" could be persisted along with its "many-to-one" relationship to the "Customer".

For sure, I don't want to create different "hbm.xml" for all my test classes, I was rather thinking of a programmatic way to generate them. Let's say something similar to the "Configuration.addClass(Class classToMap)".

Code:
public void createDummyMappingFor(Class classToMap){
/*create mapping programmatically, maybe by reading the hbm.xml associated to the classToMap and generate mapping for its dependencies*/
}


That's the beginning of an idea...


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

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.