title: Getting "no transaction is in progress" in Spring Junit test
body: <p>I'm using Spring 3.1.1.RELEASE, JUnit 4.8.1, Hibernate 4.1.0.Final, and hibernate-jpa-2.0-api.  I'm trying to run this JUnit test …</p>
  
  <pre><code>@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
  </code></pre>
  
  <p>and on the "userDao.save" line, I'm getting the fullowing exception.  I thought "AbstractTransactionalJUnit4SpringContextTests" guarantees each test to be transactional?  What can I do to solve this?</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Here are the contents of the test-context.xml file ...</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;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"&gt;
  
      &lt;context:component-scan base-package="org.mainco.subco" /&gt;
  
      &lt;!-- Define hashing properties --&gt;
      &lt;bean id="localPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
          &lt;property name="location"&gt;
              &lt;value&gt;classpath:encryption.properties&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Define test properties --&gt;
      &lt;util:properties id="applicationProperties" location="classpath:test.properties" /&gt;
  
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="org.h2.Driver" /&gt;
          &lt;property name="url" value="jdbc:h2:~/ebook-test" /&gt;
          &lt;property name="username" value="" /&gt;
          &lt;property name="password" value="" /&gt;
      &lt;/bean&gt; 
  
      &lt;bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;
          &lt;/property&gt;
          &lt;property name="persistenceXmlLocation" value="classpath*:META-INF/test-persistence.xml" /&gt;
          &lt;property name="persistenceUnitName" value="usersTestingDatabase" /&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sharedEntityManager"
          class="org.springframework.orm.jpa.support.SharedEntityManagerBean"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
      &lt;bean id="bcProvider" class="${hash.provider.class}" /&gt;
  
      &lt;bean id="jasyptStringDigester" class="org.jasypt.digest.StandardStringDigester"&gt;
          &lt;property name="algorithm"&gt;
              &lt;value&gt;${digest.algorithm}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="provider"&gt;
              &lt;ref bean="bcProvider" /&gt;
          &lt;/property&gt;
          &lt;property name="saltGenerator"&gt;
              &lt;ref bean="saltGenerator" /&gt;
          &lt;/property&gt;
          &lt;property name="saltSizeBytes" value="${salt.bytes.length}" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="saltGenerator" class="${salt.generator.class}" /&gt;
  
      &lt;!-- This Spring Security-friendly PasswordEncoder implementation will --&gt;
      &lt;!-- wrap the StringDigester instance so that it can be used from --&gt;
      &lt;!-- the security framework. --&gt;
      &lt;bean id="passwordEncoder"
          class="org.jasypt.springsecurity3.authentication.encoding.PasswordEncoder"&gt;
          &lt;property name="stringDigester"&gt;
              &lt;ref bean="jasyptStringDigester" /&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;jdbc:embedded-database id="embedded" type="H2" /&gt;
      &lt;jdbc:initialize-database data-source="dataSource"&gt;
          &lt;jdbc:script location="classpath:db-test-data.sql" /&gt;
      &lt;/jdbc:initialize-database&gt;
  
  &lt;/beans&gt;
  </code></pre>