|
#1
|
|||
|
|||
|
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 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 |
|
#2
|
|||
|
|||
|
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();
}
}
HTH. |
|
#3
|
|||
|
|||
|
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); |
|
#4
|
|||
|
|||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |