测试弹簧+ myBaits + H2数据库回滚不起作用

时间:2014-04-03 11:14:07

标签: spring junit h2 mybatis

我目前遇到问题,包括测试用例和数据库H2(我使用myBatis)。我需要做一个回滚。我配置了我的Spring上下文:

<?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"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jdbc="http://www.springframework.org/schema/jdbc"

    xsi:schemaLocation="  
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd     
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/jee
    http://www.springframework.org/schema/jee/spring-jee.xsd
    ">

    <context:annotation-config />

    <context:component-scan base-package="com.miweb.prueba.core" />


<jdbc:embedded-database id="dataSource" type="H2">
    <jdbc:script location="classpath:schemas.sql" />
    <jdbc:script location="classpath:data.sql" />
</jdbc:embedded-database>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface"
            value="com.miweb.prueba.core.dao.mybatis.SentenciasSqlMybatisMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>

</beans>

我的Junit测试是:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-configuration-test/applicationContextTest.xml")
public class DispDaoTest {

    @Autowired
    public DispositivoDao mDispDao;

@Test
public void testDelete() {  
    long cant = mDispDao.delete(1);

    assertTrue(cant >= 0);
}

@Test
public void tesUpdate() {
    Dispositivo disp = mDispDao.searchById(1);

    disp.setAge(24);

    long cant = mDispDao.update(disp);

    Dispositivo dipsBuscado = mDispDao.searchById(1);
    assertNotNull(dipsBuscado);

    assertTrue(cant >= 0);
    assertTrue(cantidadActualizados >= 0);
        assertTrue(24== dipsBuscado.getAge() );
    }
}

但是,我尝试使用注释,但它不起作用:

@Transactional
@TransactionConfiguration(defaultRollback = true)

使用@Transacional,每个测试都会失败。错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1102)
    at org.springframework.test.context.transaction.TransactionalTestExecutionListener.getTransactionManager(TransactionalTestExecutionListener.java:311)
    at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:161)
    at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:358)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

此外,我在每个测试中尝试使用@Rollback(true),但没有任何反应(如果首先弹出删除测试,则更新测试失败)。

这个错误告诉我Spring找不到“transactionManager”。我不直接使用transactionManager,我使用H2数据库。

我如何独立完成每个测试的回滚?

1 个答案:

答案 0 :(得分:2)

尝试将这些行添加到XML配置

<tx:annotation-driven transaction-manager="transactionManager"/>

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

然后重新引入@Transactional注释