4 Replies Latest reply on Mar 24, 2009 6:16 PM by diana.diana.gomez.heinsohn.com.co

    SeamTest and transactions

    taccart.thierry.accart.name

      Hi


      I'm trying to implement some tests on for my code.
      All my CRUD interface is based on SEAM and works fine.
      In addition, I've build an API to offer ability to manage data outside seam UI.


      For my tests, I'm facing the following problem : my API is a singleton that stores current entityManager and persits transcientInstance via


      getEntityManager().joinTransaction();
      getEntityManager().persist(transientInstance);
      getEntityManager().flush();
      




      I've tried to build test on this and fail to make anything with transactions.


      I finally tried the following dummy test :




      public class MyApiTest extends SeamTest {
           @Test
           public void testLM0001() {
                try {
                     UserTransaction uTx = super.getUserTransaction();
                     uTx.begin();
                     uTx.commit();
                } catch (Exception e) {
                     e.printStackTrace();
                }
           }
           
           @Test
           public void testLM0002() {
                try {
                     Transaction.instance().begin();
                     Transaction.instance().commit();
                } catch (Exception e) {
                     e.printStackTrace();
                }
           }



      and both tests are failing with error No event context active


      java.lang.IllegalStateException: No event context active
           at org.jboss.seam.ScopeType.getContext(ScopeType.java:114)
           at org.jboss.seam.Component.getInstance(Component.java:1851)
           at org.jboss.seam.Component.getInstance(Component.java:1829)
           at org.jboss.seam.Component.getInstance(Component.java:1824)
           at org.jboss.seam.transaction.Transaction.instance(Transaction.java:36)
           at org.jboss.seam.mock.BaseSeamTest.getUserTransaction(BaseSeamTest.java:1010)
           at MyApiTest.testLM0001(LogManagerTest.java:20)
           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:585)
           at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:604)
           at org.testng.internal.Invoker.invokeMethod(Invoker.java:470)
           at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:564)
           at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:830)
           at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
           at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
           at org.testng.TestRunner.runWorkers(TestRunner.java:678)
           at org.testng.TestRunner.privateRun(TestRunner.java:624)
           at org.testng.TestRunner.run(TestRunner.java:495)
           at org.testng.SuiteRunner.runTest(SuiteRunner.java:300)
           at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:295)
           at org.testng.SuiteRunner.privateRun(SuiteRunner.java:275)
           at org.testng.SuiteRunner.run(SuiteRunner.java:190)
           at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:792)
           at org.testng.TestNG.runSuitesLocally(TestNG.java:765)
           at org.testng.TestNG.run(TestNG.java:699)
           at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
           at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:122)
      




      How can I get transaction working with SeamTest ?
      Am I wrong when I'm planning to manually manage transactions with MyApi ?


      Thanks for your support :)

        • 1. Re: SeamTest and transactions
          obfuscator

          This is a tad tricky. I don't remember excactly why I did what I did, but I got it working through using


          class X extends SeamTest {
          
                  @BeforeMethod
                  @Override
                  public void begin() {
                          startTx();
                          super.begin();
                  }
          
                  @AfterMethod
                  @Override
                  public void end() {
                          super.end();
                          endTx();
                  }
          
          }
          



          I couldn't get Transaction.instance() to work, so I used InitalContext to look it up:


          return (UserTransaction) context.lookup("UserTransaction");
          



          It is possible that you can lookup the tx at the point in time that you are currently doing, but if I remember correctly there was some sort of problem with that approach.


          Good luck.

          • 2. Re: SeamTest and transactions
            javacoryd

            This error is the result of the fact that there isn't a Seam context active.


            You need to wrap your test code in this:


            new ComponentTest() {
             
                protected void testComponents() throws Exception {
                    // Test code here
                }
            }.run();

            • 3. Re: SeamTest and transactions
              taccart.thierry.accart.name

              Simply great!!! :)


              The solution for those who'd be looking for similar problems :


              public class MyApiTest extends SeamTest {
                      UserTransaction uTx = null;
              
                   @BeforeMethod
                   @Override
                   public void begin() {
                               try {
                                      uTx= (UserTransaction) super.getInitialContext().lookup("UserTransaction");
                                      uTx.begin();
                              } catch (Exception e) {
              
                                      e.printStackTrace();
                              }
                              super.begin();
                   }
              
                   @AfterMethod
                   @Override
                   public void end() {
                           super.end();
                              try {
                                              uTx.rollback(); 
                                      } catch (Exception e) {
                                              e.printStackTrace();
                                      } 
                   }
              
              @Test
              ...
              







              I will also have a look at Cory's way of running tests.



              Thanks for both :)




              • 4. Re: SeamTest and transactions
                diana.diana.gomez.heinsohn.com.co

                I am getting an exception when i try to get the tx at this point :



                uTx= (UserTransaction) super.getInitialContext().lookup("UserTransaction");




                this is the stack trace:



                java.lang.RuntimeException: PROVIDER_URL not provided in jndi.properties.  Automatic discovery not implemented yet.
                     at org.jboss.naming.JBossRemotingContextFactory.getInitialContext(JBossRemotingContextFactory.java:158)
                     at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
                     at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
                     at javax.naming.InitialContext.init(Unknown Source)
                     at javax.naming.InitialContext.<init>(Unknown Source)
                     at org.jboss.seam.util.Naming.getInitialContext(Naming.java:37)
                     at org.jboss.seam.util.Naming.initInitialContext(Naming.java:59)
                     at org.jboss.seam.util.Naming.getInitialContext(Naming.java:50)
                     at org.jboss.seam.mock.AbstractSeamTest.getInitialContext(AbstractSeamTest.java:988)
                     at com.heinsohn.fondo.test.GenerateTest.begin(GenerateTest.java:57)
                     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
                     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
                     at java.lang.reflect.Method.invoke(Unknown Source)
                     at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
                     at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:398)
                     at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:145)
                     at org.testng.internal.Invoker.invokeMethod(Invoker.java:427)
                     at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:617)
                     at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:885)
                     at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
                     at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:110)
                     at org.testng.TestRunner.runWorkers(TestRunner.java:712)
                     at org.testng.TestRunner.privateRun(TestRunner.java:582)
                     at org.testng.TestRunner.run(TestRunner.java:477)
                     at org.testng.SuiteRunner.runTest(SuiteRunner.java:324)
                     at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:319)
                     at org.testng.SuiteRunner.privateRun(SuiteRunner.java:292)
                     at org.testng.SuiteRunner.run(SuiteRunner.java:198)
                     at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:823)
                     at org.testng.TestNG.runSuitesLocally(TestNG.java:790)
                     at org.testng.TestNG.run(TestNG.java:708)
                     at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
                     at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124)



                Do i have to take care of something else?