Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > Data Access

Reply
 
Thread Tools Display Modes
  #1  
Old Aug 14th, 2004, 05:37 PM
camach camach is offline
Junior Member
 
Join Date: Aug 2004
Posts: 9
Default Unit tests and LazyInitializationException

I'm using Hibernate & Spring and I'm looking for some best practices for writing unit tests. I'm currently getting a LazyInitializationException when executing this unit test:

Code:
public void testUserInRoleOk() throws Exception {
		
		WebUser user = this.userService.retrieveWebUser("johndoe@yahoo.com", "password");
		assertTrue(this.userService.isUserInRole(user, WebRoleEnum.CUSTOMER));		
	}
The first line executes ok but then the exception is thrown in the implementation of "isUserInRole".

The WebUser object has a Hibernate relationship with WebRole, there's a WebUser.getWebRoles method that returns a Set of WebRoles and that is exactly what the "isUserInRole" method is doing (and then getting the exception).

I'm using a HibernateTransactionManager and TransactionInterceptor for both methods on the service:"retrieveWebUser" and "isUserInRole".

Now when I deploy this on a web app I guess I would use the OpenSessionInViewInterceptor but what about for unit tests? Is there an elegant way to have a Session stay open for the duration of the test method? I have a feeling I may be missing something.

Thanks
Andres
Reply With Quote
  #2  
Old Aug 14th, 2004, 05:56 PM
irbouho irbouho is offline
Senior Member
 
Join Date: Aug 2004
Location: Montréal, Canada
Posts: 848
Default

You need to simulate the OpenSessionInViewInterceptor behaviour in your Unit Tests.
I use the following class for Spring/Hibernate/JUnit tests

Code:
//Hibernate imports
import net.sf.hibernate.FlushMode;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;

//Spring imports
import org.springframework.orm.hibernate.SessionFactoryUtils;
import org.springframework.orm.hibernate.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;

//Taha imports
import org.taha.spring.BaseSpringTest;

/**
 * @author <a href="mailto:irbouh@videotron.ca">Omar Irbouh</a>
 * @version 0.1
 * @since 2004.03.03
 */
public abstract class BaseSpringHibernateTest extends BaseSpringTest {

  protected static final String SESSION_FACTORY = "sessionFactory";

  protected SessionFactory sessionFactory = null;

  protected void setUp() throws Exception {
    super.setUp();
    sessionFactory = (SessionFactory) getApplicationContext().getBean(SESSION_FACTORY);
    Session session = SessionFactoryUtils.getSession(sessionFactory, true);
    session.setFlushMode(FlushMode.NEVER);
    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
  }

  protected void tearDown() throws Exception {
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
    SessionFactoryUtils.closeSessionIfNecessary(sessionHolder.getSession(), sessionFactory);
    super.tearDown();
  }
}
org.taha.spring.BaseSpringTest is a simillar abstract class responsible for building the ApplicatinContext. You can remove the dependance on BaseSpringTest and add a method getApplicationContext() that returns the ApplicationContext.

HTH.
__________________
Omar Irbouh

Spring Modules Team
http://irbouh.blogspot.com/
Reply With Quote
  #3  
Old Aug 14th, 2004, 06:06 PM
camach camach is offline
Junior Member
 
Join Date: Aug 2004
Posts: 9
Default

Thanks! I also found a reference on the Hibernate boards:

http://forum.hibernate.org/viewtopic.php?t=929167

The example posted there does not have the line:

Code:
session.setFlushMode(FlushMode.NEVER);
Just curious on your thoughts on how FlushMode affects things.
Reply With Quote
  #4  
Old Aug 14th, 2004, 06:24 PM
irbouho irbouho is offline
Senior Member
 
Join Date: Aug 2004
Location: Montréal, Canada
Posts: 848
Default

Good point!!!

I use BaseSpringHibernateTest mainly in readonly + lazy initialization scenarios. I added this line to enforce no updates can be executed in the underlying code.

Now that Jüergen added checkWriteOperationAllowed in HibernateTemplate, I no longer need to keep this line, and the class may serve for readonly/readwrite tests. So, please remove it if you want to use the class in your tests.
__________________
Omar Irbouh

Spring Modules Team
http://irbouh.blogspot.com/
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unit Tests for data access layer kuzman Data Access 16 Oct 25th, 2007 11:42 AM
MappingLocations for Webapp and Unit tests dserodio Data Access 1 Oct 23rd, 2005 01:53 PM
Generate Unit Tests schuer Core Container 4 Oct 2nd, 2005 11:27 AM
How do you create unit tests for layered applications? paul.barry Architecture Discussion 2 May 6th, 2005 08:31 AM
Unit tests yanis97 Data Access 2 Jan 9th, 2005 09:31 PM


All times are GMT -5. The time now is 08:11 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.