1 Reply Latest reply on Jun 2, 2008 6:39 PM by quaidbrown

    JBoss Seam Web Application Testing fails with TestNG

    hebo

      Hi every body. I am trying to test a web application with testng, but having trouble to get the entityManager in my test class. This is my test class:
      public class SeamDBUnitTest extends SeamTest {


          private EntityManager entityManager;


          private EntityManagerFactory emf;


          @Test


          protected void firstTest() throws Exception {


               entityManager = getEntityManagerFactory().createEntityManager();


      ...
      ...


           }



         
         
          public EntityManagerFactory getEntityManagerFactory()


          {


              return emf;


          }


         
         
         
      @BeforeClass


          public void init()


          {


              emf = Persistence.createEntityManagerFactory("myPersistence");


          }


         
          @AfterClass


          public void destroy()


          {


              emf.close();


          }


      }



      my persistence.xml file:
      <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
        <persistence-unit name="myPersistence">   
         <provider>org.hibernate.ejb.HibernatePersistence</provider>


          <jta-data-source>java:jdbc/myDS</jta-data-source>


          <properties>


            <property name="hibernate.dialect"
                value="org.hibernate.dialect.MySQLDialect"/>


            <property name="hibernate.hbm2ddl.auto"
                value="none"/>


            <property name="hibernate.show_sql" value="true"/>


            <property name="jboss.entity.manager.factory.jndi.name"
                         value="java:/myPersistence" />


          </properties>


        </persistence-unit>


      </persistence>


      But when I run testng from eclipse I get the following error:


      FATAL org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource: java:jdbc/myDS
      javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
           at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
           at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
           at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
           at javax.naming.InitialContext.lookup(InitialContext.java:392)
           at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
           at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
           at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
           at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
           at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
           at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
           at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:713)
           at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
           at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
           at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
           at test.numbersinsight.lingraph.admin.SeamDBUnitTest.init(SeamDBUnitTest.java:53)
           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.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.invokeConfigurations(Invoker.java:82)
           at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:167)
           at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:104)
           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)
      Failed to invoke @Configuration method test.mypackage.SeamDBUnitTest.init:org.hibernate.HibernateException: Could not find datasource
      [Invoker 32389396] Invoking @BeforeMethod org.jboss.seam.mock.SeamTest.begin()
      Failed to invoke @Configuration method org.jboss.seam.mock.SeamTest.begin:Attempted to invoke a Seam component outside the an initialized application


      ***** INVOKED METHODS


           test.mypackage.SeamDBUnitTest.init() 5506056
           test.mypackage.SeamDBUnitTest.firstTest() 5506056


      *****



      Please help.

        • 1. Re: JBoss Seam Web Application Testing fails with TestNG

          I've used TestNG with a project generated by the JBoss Tools plugin for eclipse.


          With that setup, there is a separate projname-ds.xml file just for testing. It is called projname-test-ds.xml and it is in the META-INF folder in the Test project.


          You may want to track down this file in whatever setup you have, and make sure that it has the same datasources defined that your project's datasource file does.


          There may be something similar in whatever setup you are using, if you don't have the same setup that I do.


          Hope this helps!