在Spring Junit测试中获得“没有事务正在进行中”

时间:2012-12-21 14:49:10

标签: spring hibernate junit transactions

我正在使用Spring 3.1.1.RELEASE,JUnit 4.8.1,Hibernate 4.1.0.Final和hibernate-jpa-2.0-api。我正在尝试运行这个JUnit测试...

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:test-context.xml" })
public class UserDaoImplTest extends AbstractTransactionalJUnit4SpringContextTests
{
    …
    @Test
    public final void testSave()
    {
        final User user = new User();
        user.setFirstName(testProps.getProperty("test.user.first.name"));
        user.setLastName(testProps.getProperty("test.user.last.name"));
        user.setMiddleName(testProps.getProperty("test.user.middle.name"));
        user.setPassword(testProps.getProperty("test.user.password"));
        final Role role = rolesDao.findRoleByName(testProps.getProperty("test.role.teacher.name"));
        user.setRole(role);
        user.setUrl(testProps.getProperty("test.user.url"));
        user.setUserName(testProps.getProperty("test.user.username"));
        final User savedUser = userDao.save(user);
        Assert.assertNotNull(savedUser.getId());
        final User foundUser = userDao.findById(savedUser.getId());
        Assert.assertEquals(savedUser, foundUser);
    }   // testSave

在“userDao.save”行上,我得到了一个例外。我认为“AbstractTransactionalJUnit4SpringContextTests”保证每个测试都是事务性的?我该怎么做才能解决这个问题?

javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:971)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
    at $Proxy30.flush(Unknown Source)
    at org.mainco.subco.user.repo.UserDaoImpl.save(UserDaoImpl.java:118)
    at org.mainco.subco.user.test.repo.UserDaoImplTest.testSave(UserDaoImplTest.java:226)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    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:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    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:236)
    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)

以下是test-context.xml文件的内容......

<?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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    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
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <context:component-scan base-package="org.mainco.subco" />

    <!-- Define hashing properties -->
    <bean id="localPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:encryption.properties</value>
        </property>
    </bean>

    <!-- Define test properties -->
    <util:properties id="applicationProperties" location="classpath:test.properties" />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:~/ebook-test" />
        <property name="username" value="" />
        <property name="password" value="" />
    </bean> 

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="persistenceXmlLocation" value="classpath*:META-INF/test-persistence.xml" />
        <property name="persistenceUnitName" value="usersTestingDatabase" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="sharedEntityManager"
        class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

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

    <bean id="bcProvider" class="${hash.provider.class}" />

    <bean id="jasyptStringDigester" class="org.jasypt.digest.StandardStringDigester">
        <property name="algorithm">
            <value>${digest.algorithm}</value>
        </property>
        <property name="provider">
            <ref bean="bcProvider" />
        </property>
        <property name="saltGenerator">
            <ref bean="saltGenerator" />
        </property>
        <property name="saltSizeBytes" value="${salt.bytes.length}" />
    </bean>

    <bean id="saltGenerator" class="${salt.generator.class}" />

    <!-- This Spring Security-friendly PasswordEncoder implementation will -->
    <!-- wrap the StringDigester instance so that it can be used from -->
    <!-- the security framework. -->
    <bean id="passwordEncoder"
        class="org.jasypt.springsecurity3.authentication.encoding.PasswordEncoder">
        <property name="stringDigester">
            <ref bean="jasyptStringDigester" />
        </property>
    </bean>

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

</beans>

2 个答案:

答案 0 :(得分:1)

毕竟,问题在于userDao和rolesDao对象使用不同的实体管理器,一个是从上下文自动装配,另一个是在测试中实例化。

答案 1 :(得分:0)

您的配置接缝有问题:您有两个数据源! 一个明确的定义:

 <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:~/ebook-test" />
    <property name="username" value="" />
    <property name="password" value="" />
</bean> 

并定义了一个含义:

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

当我像你一样进行测试时,我的设置看起来像这样(并且它可以工作):

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

<bean
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    id="entityManagerFactory">
    <property name="persistenceUnitName" value="testH2DbPersistenceUnit" />
    <property name="dataSource" ref="dataSource" />

    <!-- this is important to connect JPA and JdbcTemplate transaction control -->
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false" />
        </bean>
    </property>
</bean>

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

BTW:将@Transactional添加到单个测试方法或测试类中(这就像将其添加到所有测试方法中)。那么您不再需要扩展AbstractTransactionalJUnit4SpringContextTests

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:test-context.xml" })
@Transactional
public class UserDaoImplTest {

   @Test  
   public test() {...}
}

然后测试方法将在最后将回滚的事务中运行。