title: Integration testing with Hibernate Envers
body: <p>I'm trying to build some tests around some audited entities. My problem is that envers only audits on a transaction commit.</p>
  
  <p>I need to create/edit some test objects, commit the transaction and then check the revisions.</p>
  
  <p>What's the best approach to integration testing with envers?</p>
  
  <p>Update: Here's a really bad, non-deterministic test class of what I want to achieve. I would prefer to do this without relying on the order of the test methods</p>
  
  <p>First create an account and account_transaction in a single transaction. Both audited entries are for revision 1.</p>
  
  <p>Second updated the account_transaction in a new transaction. The audited entry is at revision 2.</p>
  
  <p>Third, load the audited account at revision 1 and do something with it.</p>
  
  <pre><code>@Transactional
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {"/testApplicationContext.xml"})
  public class TestAuditing {
  
      @Autowired
      private AccountDao accountDao;
  
      @PersistenceContext
      private EntityManager entityManager;
  
      @Test
      @Rollback(false)
      public void first() {
          Account account = account("Test Account", "xxxxxxxx", "xxxxxx");
  
          AccountTransaction transaction = transaction(new Date(), Deposit, 100, "Deposit");
          account.setTransactions(newArrayList(transaction));
  
          accountDao.create(account);
      }
  
      @Test
      @Rollback(false)
      public void second() {
          Account account = accountDao.getById(1L);
          AccountTransaction transaction = account.getTransactions().get(0);
          transaction.setDescription("Updated Transaction");
          accountDao.update(account);
      }
  
      @Test
      public void third() {
          AuditReader reader = AuditReaderFactory.get(entityManager);
  
          List&lt;Number&gt; accountRevisions = reader.getRevisions(Account.class, 1L);
          //One revision [1]
  
          List&lt;Number&gt; transactionRevisions = reader.getRevisions(AccountTransaction.class, 1L);
          //Two revisions [1, 2]
  
          Account currentAccount = accountDao.getById(1L);
          Account revisionAccount = (Account) reader.createQuery().forEntitiesAtRevision(Account.class, 1).getSingleResult();
  
          System.out.println(revisionAccount);
      }
  </code></pre>