Announcement Announcement Module
Collapse
No announcement yet.
Unit testing with Spring and Hibernate Mocking LocalSessionFactoryBean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unit testing with Spring and Hibernate Mocking LocalSessionFactoryBean

    Code:
    <!-- Hibernate SessionFactory -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource"><ref local="myDataSource"/></property>
    		<property name="mappingResources">
                <list>
    			<value>com/jbe/orm/Person.hbm.xml</value>			
                </list>
    		</property>
    		<property name="hibernateProperties">
                
    			<props>
    				
                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
    				<prop key="hibernate.show_sql">false</prop>
                    <prop key="hibernate.connection.release_mode">on_close</prop>
    				<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
                    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                    <prop key="hibernate.cache.use_second_level_cache">true</prop>
    			</props>
    		</property>
    	</bean>
    This is my real LocalSessionFactoryBean

    Code:
    <bean id = "personDao" class = "com.jbe.orm.dao.PersonDaoImpl">
    	   <property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
    Java Code

    Code:
    public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {
    // Some code where I can use getHibernateTemplate()
    
    }
    This code performs CRUD operations which I would like to avoid for my unit tests. For for my unit tests, I would like to substitute a mock implemenation of LocalSessionFactoryBean in my test-applicationContext.xml

    I know it is possible if I extend AbstractTransactionalDataSourceSpringContextTests but for my project we don't have the spring-test jar in our class path. So we are stuck there.

    Thanks,

    MOL

  • #2
    If it is not possible to use spring-test.jar why not to use several application context files:

    Code:
    applicationContext.xml - contains beans without sessionFactory bean
    applicationContext-sf.xml - contains sessionFactory bean
    test-applicationContext.xml - contains test beans and test sessionFactory
    applicationContext.xml, applicationContext-sf.xml are used from application
    applicationContext.xml, test-applicationContext.xml are used from test

    Comment

    Working...
    X