-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate / DAO pattern / Unit testing
PostPosted: Fri Sep 25, 2009 5:41 am 
Newbie

Joined: Fri Sep 14, 2007 6:40 pm
Posts: 5
In a traditional dao pattern I can intercept all (crud) calls to from the database and so use dao mocking effectively to allow unit testing of my app logic in isolation from the db.

Can someone confirm that this is not possible once we use Hibernate and other orm (unless all the hibernate code lives inside the dao layer and we use dto's across the dao interface).

There is no longer a convenient mockable insert() or update() call being made on an interface, instead hibernate magically applies any dirty objects to the database in an arbitrary order on completion of the transaction.

----

Are there any good references on how - or whether to attempt - to unit test in light of orm?

One option might be to entirely isolate those components I want to unit test from the hibernate mapped domain model.
Express the api to those components in terms of dto's.
However this results in endless copying between the dto's and mapped objects.

----

Incidentally I see a lot of use of the word DAO in the forums - but for the most part these 'dao' are nothing but 'finders' - possibly it would be better to avoid "dao" in this case because the dao pattern usually includes insert/updat/delete.



JL


Top
 Profile  
 
 Post subject: Re: Hibernate / DAO pattern / Unit testing
PostPosted: Fri Sep 25, 2009 9:43 am 
Beginner
Beginner

Joined: Wed Jul 09, 2008 5:34 am
Posts: 41
Location: Brno, Czech Republic
Quote:
In a traditional dao pattern I can intercept all (crud) calls to from the database and so use dao mocking effectively to allow unit testing of my app logic in isolation from the db.


Not sure I understood what you mean, but you can have a hibernate.properties (and equivalents) for each environment. So, your unit test environment would point to one database (in-memory hsqldb perhaps), while the "staging" would point to a copy of your production database, and so on.


Top
 Profile  
 
 Post subject: Re: Hibernate / DAO pattern / Unit testing
PostPosted: Fri Sep 25, 2009 9:47 am 
Newbie

Joined: Fri Sep 14, 2007 6:40 pm
Posts: 5
I understand that - but that is still not 'unit testing'.
We cannot isolate the application (business) logic from the persistance mechanism.

It's not unit testing in the normal sense of the word - it is database integration testing.


Top
 Profile  
 
 Post subject: Re: Hibernate / DAO pattern / Unit testing
PostPosted: Fri Sep 25, 2009 10:10 am 
Beginner
Beginner

Joined: Wed Jul 09, 2008 5:34 am
Posts: 41
Location: Brno, Czech Republic
Basically, you want Hibernate to *not* use the database when you are testing? Unit testing database components (DAO) usually means making use of some database to make sure your units are working properly, ie: retrieving the information you asked. So, still not sure I understood what you want :-) It makes sense to use mocks for business components, but I can't think of a good case for not touching the database while testing DAO components.


Top
 Profile  
 
 Post subject: Re: Hibernate / DAO pattern / Unit testing
PostPosted: Fri Sep 25, 2009 10:52 am 
Newbie

Joined: Fri Sep 14, 2007 6:40 pm
Posts: 5
You miunderstand.

I don't want to test the DAO - thats the point.

I want to unit test the business logic that sits above the dao (in a traditional dao pattern) by mocking the dao.

However because the hibernate (orm) pattern disolves away the insert/update/delete methods on the dao api (leaving just finders) there is nothing for me to use as a hook to mock.


Top
 Profile  
 
 Post subject: Re: Hibernate / DAO pattern / Unit testing
PostPosted: Fri Sep 25, 2009 11:36 am 
Beginner
Beginner

Joined: Wed Jul 09, 2008 5:34 am
Posts: 41
Location: Brno, Czech Republic
Dude, I'm still not sure I understood what is wrong :-)

Quote:
However because the hibernate (orm) pattern disolves away the insert/update/delete methods on the dao api (leaving just finders) there is nothing for me to use as a hook to mock.


Hibernate doesn't forces you to use the insert/update/delete methods outside the DAO (ie: in your business components). I mean, you can certainly have something like:

Code:
Business => processPayment
  DAO => findByAccountNo
    Hibernate => query.list()
  DAO => insertPaymentRecord
    Hibernate => insert()
  DAO => updateBalance
    Hibernate => update()


So, your business keeps free from the implementation details of the DAO, enabling you to have N implementations of it. What am I missing? :-)


Top
 Profile  
 
 Post subject: Re: Hibernate / DAO pattern / Unit testing
PostPosted: Fri Sep 25, 2009 11:58 am 
Newbie

Joined: Fri Sep 14, 2007 6:40 pm
Posts: 5
One can also do this outside the DAO layer
account.setBalance(100);
and the DAO layer will never get called but the change still gets applied to the db.
Which of course is what you expect if you use hibernate.

And of course I can go on bashing folks head to add redundant calls like save() below ...
Account account = accountDao.find(..);
account.setBalance(100);
accountDao.save(account);
But whats the point given that Hibernate is going to persist the change anyway - with or without the call to save()..

One cannot enforce layering (there is no layering) so genuine unit testing is only possible with dto's and the two-way conversion mentioned earlier.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.