Announcement Announcement Module
Collapse
No announcement yet.
Integration testing using SpringJUnit4ClassRunner giving LazyInitializaException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Integration testing using SpringJUnit4ClassRunner giving LazyInitializaException

    Hi,

    I really need help on this one. I am using spring 3.0.0 and hibernate version 3.2.6.ga. I have my spring-configuration as follows.
    <code><?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    default-init-method="init">

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url"
    value="jdbc:oracle:thin:@ora-dev3-custom-d1.vmware.com:1521:VMDEVPTL" />
    <property name="username" value="XXVMPORTAL" />
    <property name="password" value="xxvmportal_0ne" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"
    proxy-target-class="true" />
    <context:annotation-config />
    <context:component-scan base-package="com.vmaware" />

    <!-- Transaction Manager -->
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>


    <!-- Hibernate Session Factory -->
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
    <list>
    <value>com.vmware.core.dlg.domain.DownloadGroup</value>
    <value>com.vmware.core.dlg.domain.DownloadGroupCom ment</value>
    </list>
    </property>
    <property name="mappingResources">
    <list>
    <value>com/vmware/core/sca/admin/domain/hbm/User.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.Orac le10gDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    <!-- value>hibernate.dialect=org.hibernate.dialect.Orac le9Dialect</value -->
    </property>
    </bean>

    <bean id="downloadGroupDao" class="com.vmware.core.dlg.dao.impl.DownloadGroupD aoImpl">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="downloadGroupService" class="com.vmware.core.dlg.service.impl.DownloadGr oupServiceImpl">
    <property name="downloadGroupDao" ref="downloadGroupDao" />
    </bean>


    </beans></code>

    My DAO class extends HibernateDAOSupport and i am doing all the CRUD operations using getHibernateTemplate().*** methods. Secondly i have my service layer on which i have put the @Transactional Attrbutes.....it looks like this
    <code>private IDownloadGroupDao downloadGroupDao;


    public void setDownloadGroupDao(IDownloadGroupDao downloadGroupDao) {
    this.downloadGroupDao = downloadGroupDao;
    }

    @Transactional(propagation=Propagation.REQUIRED,re adOnly=true, rollbackFor=Throwable.class)
    public DownloadGroup getDownloadGroupById(long id) throws DAOException{
    try{
    return downloadGroupDao.getDownloadGroupById(id);
    }catch(Exception e){
    String message = "Exception Occured while retriving the information for Download Group Id = "+id;
    logger.log(Level.ERROR,message);
    throw new DAOException(message, e);
    }
    }

    @Transactional(propagation=Propagation.REQUIRED,re adOnly=false, rollbackFor=Throwable.class)
    public DownloadGroup updateDownloadGroup(DownloadGroup downloadGroup)throws DAOException{
    try{
    return downloadGroupDao.updateDownloadGroup(downloadGroup );
    }catch(Exception e){
    String message = "Exception Occured while updating the information for Download Group Id = "+downloadGroup.getDownloadGroupId();
    logger.log(Level.ERROR,message);
    throw new DAOException(message, e);
    }
    }
    </code>

    Now i am writing my test case as follows:

    <code>@RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"/com/vmware/core/sca/resources/test-spring-config-blueprint.xml"})
    public class DownloadGroupServiceTest{
    private IDownloadGroupService downloadGroupService;

    @Resource
    public void setDownloadGroupService(IDownloadGroupService downloadGroupService) {
    this.downloadGroupService = downloadGroupService;
    }
    // @Test
    public void testGetDownloadGroupById() throws DAOException{
    DownloadGroup downloadGroup = downloadGroupService.getDownloadGroupById(125);
    System.out.println(downloadGroup.getBaseServerURL( ));
    System.out.println(downloadGroup.getDownloadHeader ());
    //System.out.println(downloadGroup.getCreatedBy().ge tEmailId());
    downloadGroup.setDlgFlowStatus("My New Status 123");

    }</code>

    This code gives me org.hibernate.LazyInitializationException: could not initialize proxy - no Session exception.

    I thought that when i have described my transaction on service methods, it should start a hibernate session there itself, but its not the case.

    I have attached the error log, with this post, please take a look. I can see spring loading my Service bean and also identifying the Transactional attribute.

    By the way this test case works if i add @Transaction on the test class, which i am not at all able to understand. Please clarify this doubt.
Working...
X