Announcement Announcement Module
Collapse
No announcement yet.
Clean JUNIT test with spring and hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Clean JUNIT test with spring and hibernate

    I need to set clean junit tests with Spring-1.2.6, Hibernate-3.0.5 and hsql1.7.3
    Basically, i want to launch a test on an empty database, playing all tests and getting an empty database after test set completion.
    I use the hibernate option hibernate.hbm2ddl.auto=create-drop

    When i launch the test, the schema is created : nice
    After the test is completed, the schema is not dropped: bad
    I thought that schema will be dropped at the closing of the session thanks to the hibernate.hbm2ddl.auto option.

    Here is my Junit test:

    Code:
    package coach.dao.impl.hibernate;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import coach.dao.UniteDAOIF;
    import coach.model.UniteIF;
    import coach.model.impl.basic.ModelBasicFactory;
    import coach.model.impl.basic.Unite;
    
    import junit.framework.TestCase;
    
    public class UniteDaoSpringTest extends TestCase {
    
    	ModelBasicFactory mvf;
    	ApplicationContext apc;
    	UniteDAOIF uniteDao;
    
    	protected void setUp() throws Exception {
    		super.setUp();
    		apc = new ClassPathXmlApplicationContext("coach/applicationContext.xml");
    		mvf = (ModelBasicFactory)apc.getBean("coachFactory");
    		uniteDao = (UniteDAOIF)apc.getBean("uniteDaoTarget");
    	}
    	
    	protected void tearDown() throws Exception {
    		super.tearDown();
    	}	
    
    	public void testUniteCreateHibernateSpring() throws Throwable {
    		UniteIF unJourHomme = mvf.getJourHomme();
    		Long id = uniteDao.create(unJourHomme);
    		UniteIF unJourHommeRes = (UniteIF)uniteDao.find(id);
    		assertEquals(((Unite)unJourHomme).getId(), ((Unite)unJourHommeRes).getId());
    	}
    }
    As you can see i load the spring container as an applicationContext. The problem migth be the closing of this container: how to close it properly at tearDown method ?
    If i load the spring container as a simple XmlBeanFactory i an use the destroySingletons() method but it throws an exception:

    2005-12-13 21:42:08,096 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] - <schema export unsuccessful>
    org.hibernate.HibernateException: No local DataSource found for configuration - dataSource property must be set on LocalSessionFactoryBean

    Here is my configfile:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    	<bean id="gestionnaireTache"
    		class="coach.service.impl.basic.GestionnaireTache">
    	</bean>
    	<bean id="gestionnaireRessource"
    		class="coach.service.impl.basic.GestionnaireRessource">
    	</bean>
    	<bean id="coachFactory"
    		class="coach.model.impl.basic.ModelBasicFactory">
    		<property name="gestionnaireRessource">
    			<ref bean="gestionnaireRessource" />
    		</property>
    		<property name="gestionnaireTache">
    			<ref bean="gestionnaireTache" />
    		</property>
    	</bean>
    
    	<!-- Permet de maintenir les parametres hibernate dans un fichier properties -->
    	<bean id="placeholderConfig"
    		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location">
    			<value>/hibernate.properties</value>
    		</property>
    	</bean>
    
    	<!-- DataSource defintion -->
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName">
    			<value>${hibernate.connection.driver_class}</value>
    		</property>
    		<property name="url">
    			<value>${hibernate.connection.url}</value>
    		</property>
    		<property name="username">
    			<value>${hibernate.connection.username}</value>
    		</property>
    		<property name="password">
    			<value>${hibernate.connection.password}</value>
    		</property>
    	</bean>
    
    
    	<!-- Session factory pour Hibernate -->
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="mappingResources">
    			<list>
    				<value>coach/model/impl/basic/Unite.hbm.xml</value>
    				<value>
    					coach/model/impl/basic/Description.hbm.xml
    				</value>
    			</list>
    		</property>
    		<property name="mappingDirectoryLocations">
    			<list>
    				<value>coach/model/impl/basic</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					${hibernate.dialect}
    				</prop>
    				<prop key="hibernate.show_sql">
    					${hibernate.show_sql}
    				</prop>
    				<prop key="hibernate.max_fetch_depth">
    					${hibernate.max_fetch_depth}
    				</prop>
    				<prop key="hibernate.hbm2ddl.auto">
    					${hibernate.hbm2ddl.auto}
    				</prop>
    				<prop key="hibernate.cache.provider_class">
    					${hibernate.cache.provider_class}
    				</prop>
    			</props>
    		</property>
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    	</bean>
    
    	<!-- Gestionnaire de transaction pour Hibernate -->
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    	<!-- DAO pour la gestion des Unites -->
    	<bean id="uniteDaoTarget"
    		class="coach.dao.impl.hibernate.UniteDao">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="txProxyTemplate" abstract="true"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager" ref="transactionManager" />
    		<property name="transactionAttributes">
    			<props>
    				<prop key="*">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
    
    	<bean id="uniteDaoProxy" parent="txProxyTemplate">
    		<property name="target">
    			<ref bean="uniteDaoTarget" />
    		</property>
    	</bean>
    
    </beans>

  • #2
    You might find these helpful: http://www.springframework.org/docs/...e/testing.html
    http://today.java.net/pub/a/today/20...e-mapping.html

    Basically, you want to extend http://www.springframework.org/docs/...textTests.html

    Comment


    • #3
      Thanks for the URL, but...

      I tried to set up a junit test based on spring-mock classes.
      I get exactly the same problem !

      Code:
      package coach.dao.impl.hibernate;
      
      import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
      
      import coach.dao.UniteDAOIF;
      import coach.model.UniteIF;
      import coach.model.impl.basic.ModelBasicFactory;
      import coach.model.impl.basic.Unite;
      
      public class UniteDaoSpringContextTest extends AbstractTransactionalDataSourceSpringContextTests {
      	
      	ModelBasicFactory mvf;
      	UniteDAOIF uniteDao;
      	
      	protected String[] getConfigLocations() {
      		return new String[] {"classpath:/coach/applicationContext.xml"};
      	}
      	
      	protected void onSetUpBeforeTransaction() throws Exception {
      		mvf = (ModelBasicFactory)applicationContext.getBean("coachFactory");
      		uniteDao = (UniteDAOIF)applicationContext.getBean("uniteDaoTarget");
      		setComplete();
      	}
      	
      	protected void onTearDownAfterTransaction() throws Exception {
      		// TODO Auto-generated method stub
      		super.onTearDownAfterTransaction();
      	}
      
      	public void testUniteCreateHibernateSpring() throws Throwable {
      		UniteIF unJourHomme = mvf.getJourHomme();
      		Long id = uniteDao.create(unJourHomme);
      		UniteIF unJourHommeRes = (UniteIF)uniteDao.find(id);
      		assertEquals(((Unite)unJourHomme).getId(), ((Unite)unJourHommeRes).getId());
      	}	
      }

      Comment

      Working...
      X