@Transactional不会在DAO @Test方法上回滚

时间:2018-08-21 13:03:27

标签: java mysql spring

我创建了一个 MySQL数据库并填充了用于测试目的的行。我想在此数据库上进行DAO单元测试。每个Test2@Test,因此在每个测试之后都会进行回滚。不幸的是,由于仍对我的数据库进行了更改,因此无法正常工作。

我正在使用以下context.xml加载我的 Spring 配置

@Transactional

This stackoverflow问题说我

  

必须在应用程序上下文中提供一个<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> bean

但是即使有了它(在我的上下文中为PlatformTransactionManager),也什么也没有发生,我的数据库仍然被修改并且没有回滚。

这是我的DAO测试班

transactionManager

我在配置理解中缺少关于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"); } } 应该如何工作的信息吗?

1 个答案:

答案 0 :(得分:1)

如@ 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>
相关问题