I have created Spring integration test in my application (Spring/Hibernate/H2)
The problem is, one test doesn't rollback properly, leaves some stuff in database and causes subsequent tests to fail.
I have noticed, that tests works good if persisted entity is simple entity.
The test fails, when entity is part of inheritance hierarchy and inheritance type is of type InheritanceType.JOINED.
When I change it to InheritanceType.SINGLE_TABLE it desn't fail.
The test looks like this:
The entity class is:Code:@RunWith(SpringJUnit4ClassRunner.class) @Transactional @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) @ContextConfiguration(locations = {"classpath:beans/repository-beans.xml"}) public class RollbackProblemSpringTest { @PersistenceContext private EntityManager entityManager; @Test //PASSES public void isDBEmptyTest_1() throws Exception { Query countQuery = entityManager.createQuery("select count(qi) from " + ParentEntity.class.getSimpleName() + " qi"); int elementsCount = ((Long)countQuery.getSingleResult()).intValue(); assertThat(elementsCount).isEqualTo(0); } @Test //PASSES public void testThatInfluencesOtherTests() { ParentEntity queueItem = new SecondChildEntity(1); entityManager.persist(queueItem); ParentEntity queueItem1 = new SecondChildEntity(2); entityManager.persist(queueItem1); // given flush(); // when Query deleteQuery = entityManager.createQuery("delete from " + SecondChildEntity.class.getSimpleName()); deleteQuery.executeUpdate(); flush(); // then Query countQuery = entityManager.createQuery("select count(qi) from " + ParentEntity.class.getSimpleName() + " qi"); int elementsCount = ((Long)countQuery.getSingleResult()).intValue(); assertThat(elementsCount).isEqualTo(0); } @Test //FAILS public void isDBEmptyTest_2() throws Exception { Query countQuery = entityManager.createQuery("select count(qi) from " + ParentEntity.class.getSimpleName() + " qi"); int elementsCount = ((Long)countQuery.getSingleResult()).intValue(); assertThat(elementsCount).isEqualTo(0); } private void flush() { entityManager.flush(); entityManager.clear(); } }
Spring configuration is:Code:@javax.persistence.Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class ParentEntity { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; public ParentEntity() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } }
You can also downlaod complete maven project: project.zipCode:<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="no.mintra.offlinetrainingportal.infrastructure.persistence" /> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> <property name="dataSource" ref="H2DataSource" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> <property name="dataSource" ref="H2DataSource" /> <property name="jpaVendorAdapter" ref="H2JpaVendorAdaptor" /> <property name="jpaPropertyMap"> <map> <entry key="hibernate.hbm2ddl.auto" value="create-drop" /> </map> </property> </bean> <bean id="H2DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.h2.Driver"/> <property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MYSQL;LOCK_MODE=3"/> </bean> <bean id="H2JpaVendorAdaptor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> <property name="generateDdl" value="true" /> <property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" /> </bean> </beans>


Reply With Quote
