休眠-事务不会在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");
}
}
}
我可以在控制台“ Account again is null”上看到该消息.由于其在测试中.但是测试完成之后.在数据库中,标识为“ 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>
解决方法:
您的测试看起来绝对不可思议. @ContextConfiguration已经加载了应用程序上下文,您无需手动进行操作.
以下代码应按预期工作:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/testApplicationContext.xml"})
@TransactionConfiguration(defaultRollback=true)
public class TestDummy {
@Autowired
private AccountManager accountManager;
@Test
@Transactional
public void testDeleteAccount(){
Account acc = accountManager.getAccountDaoHibernate().get("87EDA29EBB65371CE04500144F54AB6D");
System.out.println("Account name is "+acc.getAccountName());
accountManager.deleteAccountHard(acc);
Account acc1 = accountManager.getAccountDaoHibernate().get("87EDA29EBB65371CE04500144F54AB6D");
if(acc1 != null){
System.out.println("Now name is "+ acc1.getAccountName());
}else{
System.out.println("Account again is null");
}
}
}
也可以看看:
> 9.3.5 Spring TestContext Framework
标签:spring-test,hibernate,junit4,spring 来源: https://codeday.me/bug/20191102/1994771.html