lundi 23 novembre 2015

Overriding datasource of a hibernate sessionfactory in test cases

I have a hibernate SessionFactoryBean configured in my applicationContext which already has a real datasource as property. But i would like to use another local/mock datasource in my test class by overriding the actual datasource which was already injected to the sessionfactory.

The bean definition in the main application Context is like this.

<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
            abstract="false" scope="singleton" lazy-init="default" autowire="default">
        <property name="dataSource" ref="pcfDataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">$`db.hibernate.dialect}   </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
            </props>
        </property>
</bean>

I have a local datasource like this in my test case:

template = InitializeDataSource.getInitializedDataSource();
DataSource dataSource = template.getDataSource; 

I need to override the injected datasource with the one in the bottom

Aucun commentaire:

Enregistrer un commentaire