spring+Transaction+unitTest

spring + jdbcTemplate + Transaction + unitTest , 今天在用最基本的Spring + Transaction写单元测试的时候,在单元测试方法上面加了@Transactional但是事务一直回滚,各种google最终找到解决方法,记录如下。

pom

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
</dependency>

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.1.10</version>
</dependency>

xml 配置 DataSource 和 TransactionManager

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/tx
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
"
       xmlns:tx="http://www.springframework.org/schema/tx">

    <!-- 定义template组件 -->
    <bean id="jdbcTemplate"
          class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- 注入连接信息 -->
        <property name="dataSource" ref="dataSource">
        </property>
    </bean>

 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="driverClass" value="${driverClass}"></property>
    </bean>

    <!-- 配置spring的PlatformTransactionManager,名字为默认值 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 开启事务控制的注解支持 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

单元测试

@ContextConfiguration(locations = { "classpath:jdbctemplate-pg.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class })
public class SpringTestBase extends AbstractJUnit4SpringContextTests {
}
public class TxTest extends SpringTestBase{

    @Autowired
    private JdbcTemplate jdbcTemplate;


    //Transaction test
    @Transactional(rollbackFor=Exception.class)
    @Test
    @Rollback(true)
    public void transactionTest(){
        jdbcTemplate.update("insert into test(name, sex) values (?, ?)", new Object[]{
                "test-1", "test-1"
        });


        int i = 1 / 0;

        jdbcTemplate.update("insert into test1(name, sex) values (?, ?)", new Object[]{
                "test-2", "test-2"
        });

    }

}

核心细节 如果没有如下代码


@TestExecutionListeners({
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class })

就不会回滚。所以一定要加上这个

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
慷慨打赏