@Transactionalは、DAO @Test法にロールバックしません。

VictorGalisson:

私が持っているMySQLデータベースを作成し、テスト目的の行でいっぱい。私は、このデータベースに私のDAOのユニットテストをしたいです。それぞれが@Testされる@Transactionalので、ロールバックが各テストの後に行われます。変更はまだ私のデータベースに対して行われたとして残念ながら、それが機能していません。

私は私のロードてる春の次のcontext.xmlで設定を

<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:jdbc="http://www.springframework.org/schema/jdbc"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                                                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                                                    http://www.springframework.org/schema/jdbc
                                                    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"
            xmlns:tx="http://www.springframework.org/schema/tx">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
        <property name="url" value="jdbc:mysql://localhost:3306/push_test" />  
        <property name="username" value="push_dao" />  
        <property name="password" value="pushpassword" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource"  ref="dataSource" />    
    </bean>

    <bean id="userPushDAO" class="my.package.userPushDAOImpl">
         <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

この stackoverflowの問題は、私と言いました

提供しなければならないPlatformTransactionManagerアプリケーションのコンテキストでBeanを

さえ(それはそれでだ。しかしtransactionManager、私のコンテキスト内で)何も起こりません、私のデータベースにはまだ修正され、ロールバックされていません。

ここに私のDAOのテストクラスがあります

public class UtilisateurPushDAOImplTest {

    private static ApplicationContext ctx;

    private static UserPushDAO userPushDAO;


    @BeforeClass
    public static void doSetup() {
        ctx = new ClassPathXmlApplicationContext("context.xml");
        userPushDAO = (userPushDAO) ctx.getBean("userPushDAO");
    }

    @Test
    @Transactional
    public void test() {
         userPushDAO.deleteById("id");
    }
}

私は私の中で何かが足りない設定や、私の中の理解どのように/何が@Transactional動作するはずです/やるの?

VictorGalisson:

@としてM.Deinumは、コメント欄に投稿しました

@Transactionalテストでのみ動作しますSpringRunnerユニットテストランナーとして。

あなたは、アプリケーション・コンテキストを自分で作成する場合は動作しません。

また、追加することを忘れないでくださいトランザクションスキーマをcontext.xml正しくコンテキストファイルをロードできるようにします。

DAOのテストクラス

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/context.xml"})
public class UserPushDAOImplTest {

    @Resource(name="userPushDAO")
    private UserPushDAO userPushDAO;

    @Test
    @Transactional
    public void test() {
         userPushDAO.deleteById("id");
    }
}

context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:jdbc="http://www.springframework.org/schema/jdbc"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                                http://www.springframework.org/schema/beans/spring-beans.xsd
                                http://www.springframework.org/schema/jdbc
                                http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
                                http://www.springframework.org/schema/context
                                http://www.springframework.org/schema/context/spring-context.xsd">  

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
        <property name="url" value="jdbc:mysql://localhost:3306/push_test" />  
        <property name="username" value="push_dao" />  
        <property name="password" value="pushpassword" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource"  ref="dataSource" />    
    </bean>

    <bean id="userPushDAO" class="my.package.userPushDAOImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=218810&siteId=1