Hi,
I have been researching this problem for ages with no solution, and perhaps there is something I dont understand, or am missing.
My app is using spring 3.1 and Hibernate 4. I have created an integration test with JUnit 4 as follows (just as an example) and running in STS:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:mobileu/system-test-config.xml")
@TransactionConfiguration
@Transactional
When running the test, the testCreateApplicationDefinition1 method runs fine, but the testCreateApplicationDefinition2 method fails with a "org.hibernate.SessionException: Session is closed!".Code:public class MobileUTests { @Autowired private ApplicationDefinitionService applicationDefinitionService; @Before public void setUp () { } @Test /** * Test that a new ApplicationDefinition object can be created and saved to the database. */ public void testCreateApplicationDefinition1() { String name = "Application Name"; String description = "Application Description"; ApplicationDefinition applicationDefinition = new ApplicationDefinition(); applicationDefinition.setName(name); applicationDefinition.setDescription(description); applicationDefinitionService.saveApplicationDefinition(applicationDefinition); } @Test /** * Test that a new ApplicationDefinition object can be created and saved to the database. */ public void testCreateApplicationDefinition2() { String name = "Application Name"; String description = "Application Description"; ApplicationDefinition applicationDefinition = new ApplicationDefinition(); applicationDefinition.setName(name); applicationDefinition.setDescription(description); applicationDefinitionService.saveApplicationDefinition(applicationDefinition); }
If I annotate with @DirtiesContext, then the context is re-created between tests and it works fine. However, I don't want to re-load the context between tests.
What am I missing, why is the hibernate session closing between tests? How am I supposed to run integration tests with Spring 3.1 and Hibernate ? I have tried both Hibernate 3 and 4.
Config below:
Code:<context:component-scan base-package="mobileu" /> <context:annotation-config /> <tx:annotation-driven/> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan"> <list> <value>mobileu/domain</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.format_sql=true hibernate.show_sql=true hibernate.hbm2ddl.auto=update hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect </value> </property> </bean> <!-- A transaction manager for working with Hibernate SessionFactories --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <context:property-placeholder location="classpath:mobileu/systemtest.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.driverClassName}"/> <property name="url" value="${db.url}"/> <property name="username" value="${db.userName}"/> <property name="password" value="${db.password}"/> <property name="maxActive" value="100"/> <property name="maxIdle" value="30"/> <property name="maxWait" value="1000"/> <property name="defaultAutoCommit" value="true"/> </bean>


Reply With Quote
