camach
Aug 14th, 2004, 06:37 PM
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:
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
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