3 Replies Latest reply on Mar 24, 2010 4:56 PM by adamw

    Using Envers in transactional Unit Tests

      Hi,

       

      what is the best practice to use Envers in a transactional unit test, e.g. for testing the integration of Envers with a service layer?

       

      In my current project we use Spring 2.5.6, Hibernate 3.3.2, Hibernate Annotations and of course Envers. The unit tests for our application are transactional, so that changes done to the database are rolled back and do never get committed (we base our unit tests on the convenience test class org.springframework.test.jpa.AbstractJpaTests)

       

      The problem was that due to the fact that the transaction never gets committed the AuditSync.beforeCompletion() method never gets called, thus Envers never wrote data which makes it impossible to check if data got revisioned correctly.

       

      Our whacky workaround was to add a method to our test class that forces the AuditSync.beforeCompletion() to get called so Envers writes to the ausiting tables but still at the end of a test run everything gets rolled back nicely... still, it feels wrong somehow - or at least like unsupported hacking.

       

      So how do other handle this?

       

      Dirk.

        • 1. Re: Using Envers in transactional Unit Tests
          adamw

          I normally use a test database for my tests (e.g. an in-memory H2 or sth like that) so I don't use the rollback approach, so I guess I won't be much of a help here.

           

          Considering the way you are doing the tests I don't see any other option than calling the AuditSync explicitly. You could also create your own transaction synchronization, register it before the audit sync (so right a the beginning of the TX), and test/rollback there, but that's essentially the same as your solution. However you won't be able to test more than one revision that way, as it's one revision per transaction

           

          Adam

          • 2. Re: Using Envers in transactional Unit Tests

            Dirk, Adam,

             

            How would I go about getting an instance of AuditSync in the unit tests?

             

            The org.hibernate.event.EventSource seems like the most challenging part to get a hold of in the unit test method...

             

            Thanks,

            John

            • 3. Re: Using Envers in transactional Unit Tests
              adamw

              Why would you want an AuditSync in unit test?