どういうわけか、Spring Testを実行するときに、私のテストは削除トランザクションをロールバックしていません。データは完全に削除されます。Spring-Hibernateコンボを使用しています。
これが私のテストクラスです:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners( {TransactionalTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class
})
@ContextConfiguration(locations={"/testApplicationContext.xml"})
@TransactionConfiguration(defaultRollback=true)
public class TestDummy {
private ApplicationContext context;
@Transactional
private AccountManager getAccountManager() {
this.context = new ClassPathXmlApplicationContext("testApplicationContext.xml");
return (AccountManager) context.getBean("accountManager");
}
@Test
@Transactional
@Rollback(true)
public void testDeleteAccount(){
Account acc = getAccountManager().getAccountDaoHibernate().get("87EDA29EBB65371CE04500144F54AB6D");
System.out.println("Account name is "+acc.getAccountName());
getAccountManager().deleteAccountHard(acc);
Account acc1 = getAccountManager().getAccountDaoHibernate().get("87EDA29EBB65371CE04500144F54AB6D");
if(acc1 != null){
System.out.println("Now name is "+ acc1.getAccountName());
}else{
System.out.println("Account again is null");
}
}
}
コンソールに「Accountagainisnull」というメッセージが表示されます。テストでのように。しかし、テストが終了した後。データベースでは、IDが「87EDA29EBB65371CE04500144F54AB6D」のレコードが完全に削除されます。テストが完了した後、ロールバックする必要があります。トランザクションがロールバックされない理由は本当に混乱しています。
これが私のtestApplicationContext.xmlエントリです:
<bean id="accountManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target"><ref local="accountManagerTarget"/></property>
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="transactionAttributes">
<props>
<!-- Avoid PROPAGATION_REQUIRED !! It could kill your performances by generating a new transaction on each request !! -->
<prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
</props>
</property>
<property name="preInterceptors">
<list>
<ref bean="hibernateInterceptor"/>
</list>
</property>
</bean>
<bean id="accountManagerTarget"
class="com.db.spgit.abstrack.manager.AccountManager">
<property name="accountDaoHibernate" ref="accountDaoHibernate" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="configLocation">
<value>classpath:hibernate-test.cfg.xml</value>
</property>
</bean>
<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>