7 Replies Latest reply on Feb 9, 2012 1:20 PM by javacoryd

    SeamTest with transactional roll back

    johnnyren

      Is it possible to configure SeamTest with automatic transactional roll back after each session bean Hibernate integration test so that I don’t have to spent time writing database data cleaning code? 

        • 1. Re: SeamTest with transactional roll back
          pmuir

          Take a look at SeamTest with DBUnit

          • 2. Re: SeamTest with transactional roll back
            skallio

            I have been trying to find information for the same question. Can somebody give more specific instruction what to do?


            The difference in my case is that I don't have session beans but I would like to run SeamTests in transaction and rollback after each test. (I wonder if this is not what everybody wants?)

            • 3. Re: SeamTest with transactional roll back
              davidmiller

              I wouldn't mind an answer to this problem as well...  The SeamTest and DBUNit docs tell me nothing....and if they do, what am I missing?

              • 4. Re: SeamTest with transactional roll back
                davidmiller

                Bump bump

                • 5. Re: SeamTest with transactional roll back
                  javacoryd

                  We have been doing this for quite some time now.  We converted our base seam test over to JUnit because we like JUnit.  You can do this with TestNG as well.  Note: this is of course using a ComponentTest.  Here is what we do:

                   

                  @Before ( before the test starts )

                   

                  a) Get the entityManager

                  b) Start the transaction

                  EntityTransaction transaction = entityManager.getTransaction();

                  if ( !transaction.isActive() ) {

                       transaction.begin();

                  }

                  c) Put the tranasaction into ThreadLocal

                   

                  @After ( upon test completion )

                   

                  a) Get the transaction off of ThreadLocal

                  b) Rollback the transaction

                  if ( trans != null && trans.isActive() ) {

                       trans.rollback();

                  }

                  c) Remove transaction from ThreadLocal

                   

                  Cory.

                  • 6. Re: SeamTest with transactional roll back
                    davidmiller

                    Thanks Cory, I'll give that a go.

                    • 7. Re: SeamTest with transactional roll back
                      javacoryd

                      If you are willing to change the method you override in the ComponentTest to something else, this is easier:

                       

                      public abstract class BaseTransactionalTest extends SeamTest {

                       

                          protected abstract class TransactionalComponentTest extends ComponentTest {

                       

                              protected void testComponents() throws Exception {

                                  Transaction.instance().begin();

                                  try {

                                      doTestComponents();

                                  } finally {

                                      Transaction.instance().rollback();

                                  }

                              }

                       

                              protected abstract void doTestComponents() throws Exception;

                       

                          }

                      }