3

どういうわけか、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>
4

1 に答える 1

4

あなたのテストは絶対に奇妙に見えます。@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");
        }           
    }
}

参照:

于 2011-02-24T19:57:25.030 に答える