-->
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.  [ 2 posts ] 
Author Message
 Post subject: Scope of transaction in Unit Tests
PostPosted: Tue Jul 21, 2009 11:34 am 
Newbie

Joined: Fri Jul 17, 2009 10:37 am
Posts: 3
I am a newbie so this is essentially a best practice question. I have a simple object, unit tests for the object in a VS Unit Test project and an NHibernateSessionManager object that does basic BuildSessionFactory(), OpenSession(), BeginTransaction(), etc. calls.

What I thought would be correct is for the unit test fixture to start a single transaction in the ClassInitialize method, perform the various database requests/changes in all the tests then Rollback the single transaction in the ClassCleanup method but it does not work. It appears to work if I roll the transaction back because nothing is left in the SQL database but the changes are not commited even if I test with CommitTransaction instead. Walking through the logic the transaction is no longer active after one test has been completed.

I can make it work correctly by using a separate transaction for each test...that is, wrap each test in Begin/CommitTransaction but that obviously creates a separate transaction for each test.

Is a separate transaction for each test the correct solution or am I doing something wrong to make the transaction close before I expect? The answer does not seem as important for the unit tests but makes me wonder why the transaction is not active and how that would affect the "real" application.


Top
 Profile  
 
 Post subject: Re: Scope of transaction in Unit Tests
PostPosted: Fri Jul 24, 2009 11:19 am 
Newbie

Joined: Fri Jul 24, 2009 11:06 am
Posts: 8
A transaction per test method is probably the most flexible approach. But usually we want all test cases to rollback.


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