-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problems with unit tests and NHibernate
PostPosted: Tue Feb 20, 2007 2:33 pm 
Newbie

Joined: Thu May 04, 2006 2:42 pm
Posts: 10
Location: Columbus, OH
I'm having some problems with unit testing while using NHibernate. Here's the basic flow of my tests:

1) Create some objects
2) Save the objects
3) Put the object in a state so that it won't pass business rules and verify that the validation rules are working
4) Try and delete all of the objects that I had saved to the database and restore everything to the way it was before I ran the test

The problem happens when I do step 4... in the process of putting everything back, I'm executing a query. But before NHibernate attempts to execute the query, it tries to flush all dirty objects in the session, which fails because some of the objects that I was testing are not valid (I have implemented IInterceptor and I do my validation in OnFlushDirty()).

So my question is what can I do here? It seems that I can't do anything using NHibernate if I have an object that is invalid in the session because it's going to fail every time it tries to flush it, which will happen any time I do anything.

I know about the ISession.FlushMode property, but that doesn't help me out because the problem still exists, even though you might not run into it as much.

Any thoughts?

Jon

_________________
Jon Kruger
http://jonkruger.com/blog


Top
 Profile  
 
 Post subject: Unit Tests and NHibernate
PostPosted: Tue Feb 20, 2007 6:24 pm 
Beginner
Beginner

Joined: Wed Nov 29, 2006 5:33 pm
Posts: 28
Location: Chicago, IL
In most of our tests, we open a session and begin a transaction in the SetUp. In our TearDown, we rollback the transaction and close the session.

This seems like it will work for you since you don't want the invalid objects in the database.

In those few instances where we are actually testing that the database is performing a task correctly, we either leave the transaction management out of the SetUp/TearDown, or we just make sure to leave the transaction and session in a state where the TearDown can still close the session safely.

_________________
Chuck

Not in the face! Not in the face!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 7:14 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
If you are using sql 2005 you can use TransactionScope to have your tests rolled back automatically:


Code:
IOrgRepository orgRepository = IocContainer.Resolve<IOrgRepository>();

using(TransactionScope scope = new TransactionScope()){
            //create a new org
            Organization newOrg = new Organization(Functions.RandomString(10));

            //add the org into the Repository
            orgRepository.Add(newOrg);
            
            //clear the session so that we can retrieve the item from the data repository
            ((NHibernateOrgRepository)orgRepository).SessionContext.CurrentSession.Clear();

            //retrieve the org from the data store
            Organization reconstitutedOrg = orgRepository.GetById(newOrg.Id);
            
            //verify that retieved org is equal to the new org
            Assert.AreEqual(newOrg.Id, reconstitutedOrg.Id);
         }


You'll have to change the connection release mode to not use the aggresive release style to avoid the transaction rolling up to msdtc.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 1:00 am 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Evict the invalid objects from the session.

Session.Evict()


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