Spring @Transactional方法不会回滚

时间:2015-01-20 12:48:58

标签: java spring hibernate jpa testing

我向Spring,hibernate和testng提出了一个问题。

我正在开发一个应用程序并尝试编写事务单元测试。问题是当我的buissnes方法被标记为" transactional"?

时,我怎么能回滚我的数据库操作?

代码:

@Test
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@TransactionConfiguration(defaultRollback = true)
public class SampleTest extends
AbstractTransactionalTestNGSpringContextTests {


@Autowired
private AuthorDao authorDao; 

@BeforeTest
void createAppCtx() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            "/applicationContext.xml");

}


@Test
void testStg() {

    Person person = new Author();
    person.setFirstName("Edward");
    person.setLastName("Kowalski");
    authorDao.createAuthor(person);
}

在我的作者中我有以下方法:

@Repository
@Transactional
public class AuthorDao {

@PersistenceContext
private EntityManager entityManager;


public AuthorDao() {

}

public AuthorDao(EntityManager entityManager) {
    this.entityManager = entityManager;
}

public Author createAuthor(Person author) {
    entityManager.persist(author);
    return (Author) author;
}

}

如果需要应用程序上下文,我也可以附加它。
因此,您可以看到buisness方法是事务性的,因此在调用之后会有一个提交。那么关键是如何避免在测试类中提交? 可能吗?

非常感谢您的帮助。

编辑: 的applicationContext:

  <?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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    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-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


<context:component-scan base-package="pl.hs" />
<mvc:annotation-driven />
<tx:annotation-driven transaction-manager="myTxManager" /> 


<beans>


    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>


    <bean id="jdbcPropertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="classpath:project.properties" />

    <bean id="myDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${hibernate.connection.driver_class}" 
        p:url="${hibernate.connection.url}"
        p:username="${hibernate.connection.username}" 
        p:password="${hibernate.connection.password}" />


    <bean id="persistenceUnitManager"
        class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="persistenceXmlLocations">
            <list>
                <value>classpath*:META-INF/persistence.xml</value>
            </list>
        </property>
        <property name="defaultDataSource" ref="myDataSource" />
    </bean>

    <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="persistenceUnitName" value="pl.hs" />
    </bean>

    <bean id="myTxManager" name="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEmf" />
        <!-- <property name="dataSource" ref="myDataSource" /> -->
    </bean>

</beans>
</beans>

Persistnce.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                            http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

<persistence-unit name="pl.hs"
    transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <class> myJavaClasses </class>




    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>

        <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"></property>
        <property name="hibernate.show_sql" value="true" />



        <property name="hibernate.hbm2ddl.auto" value="create"></property>
    </properties>


</persistence-unit>

2 个答案:

答案 0 :(得分:0)

将@Transactional添加到测试方法中,以便应用TransactionConfiguration。

答案 1 :(得分:0)

TransactionConfiguration在测试开始时启动一个新事务,并在测试结束时将其回滚。

所以你不必做任何特别的事情来推动当前的交易。

如果您想在以下后添加另一个逻辑单元:

authorDao.createAuthor(person);

然后你最好再编写另一种测试方法。

每个测试都应该验证一个且只有一个行为单元。如果您在一个测试方法中测试了几个职责,那么您应该将它们分成几个测试。

相关问题