3 Replies Latest reply on May 15, 2007 6:07 PM by Sheldon s

    PersistenceException during testNG unit test

    Arbi Sookazian Master

      Based on the following persistence.xml file (which is located in my Eclipse project's resources/META-INF folder):

      <?xml version="1.0" encoding="UTF-8"?>

      <persistence-unit name="RSDB"> org.hibernate.ejb.HibernatePersistence
      <jta-data-source>java:jdbc/RespaJbossDS</jta-data-source>



      <!-- These are the default for JBoss EJB3, but not for HEM: -->



      </persistence-unit>



      I get the following excpetion in the testNG plugin window in Eclipse when I run my testNG test suite:

      javax.persistence.PersistenceException: No Persistence provider for EntityManager named RSDB
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:41)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
      at example1.DashboardActionTest.init(DashboardActionTest.java:53).......

      I am trying to manually inject the EntityManager instance into the Action class that is being tested b/c otherwise I get nullpointerexception.

      Here's the Test class code:

      package example1;

      import javax.persistence.EntityManager;
      import javax.persistence.EntityManagerFactory;
      import javax.persistence.Persistence;

      import org.testng.annotations.Configuration;
      import org.testng.annotations.Test;

      import com.h123.loantrack.DashboardAction;
      import com.h123.loantrack.User;

      public class DashboardActionTest
      {
      @Test
      public void testDashboardAction()

      {
      EntityManager em = getEntityManagerFactory().createEntityManager();
      //em.getTransaction().begin();

      User user = new User();
      user.setUserID(10471);

      DashboardAction action = new DashboardAction();

      action.setUser(user);
      action.setRSDB(em);

      assert "main".equals(action.findAppLoanRecords());

      assert "main".equals(action.findPQLoanRecords());

      assert "main".equals(action.findQRWLoanRecords());

      assert "main".equals(action.findWFLoanRecords());

      //em.getTransaction().commit();
      //em.close();
      }


      private EntityManagerFactory emf;

      public EntityManagerFactory getEntityManagerFactory()
      {
      return emf;
      }

      @Configuration(beforeTestClass=true)
      public void init()
      {
      emf = Persistence.createEntityManagerFactory("RSDB");
      }

      @Configuration(afterTestClass=true)
      public void destroy()
      {
      emf.close();
      }


      }

      Anybody know how to fix this?

      I found the following info from http://www.hibernate.org/hib_docs/entitymanager/reference/en/html/configuration.html:

      When Persistence.createEntityManagerFactory() is called, the persistence implementation will search your classpath for any META-INF/persistence.xml files using the ClassLoader.getResource("META-INF/persistence.xml") method. Actually the Persistence class will look at all the Persistence Providers available in the classpath and ask each of them if they are responsible for the creation of the entity manager factory manager1. Each provider, from this list of resources, it will try to find an entity manager that matches the name you specify in the command line with what is specified in the persistence.xml file (of course the provider element must match the current persistent provider). If no persistence.xml with the correct name are found or if the expected persistence provider is not found, a PersistenceException is raised.

      So is my persistence.xml not in the classpath? Running test suite inside Eclipse without JBoss running.

      thanks.

        • 1. Re: PersistenceException during testNG unit test
          Arbi Sookazian Master

          problem resolved by moving persistence.xml around into various folders in Eclipse.

          now getting HibernateException: could not find datasource. I am referencing the persistence unit (Entity Manager) as follows:

          @Configuration(beforeTestClass=true)
           public void init()
           {
           emf = Persistence.createEntityManagerFactory("RSDB");
           }
          
          The name of the datasource file is "loantrack-ds.xml" and is as follows:
          
          <?xml version="1.0" encoding="UTF-8"?>
          
          <datasources>
           <local-tx-datasource>
           <jndi-name>jdbc/RespaJbossDS</jndi-name>
           <jndi-name>java:jdbc/RespaJbossDS</jndi-name>
           <connection-url>jdbc:JSQLConnect://d-rtl-db-01:2233/database=RSDB/user-name=process-jboss/password=pjss986sPROD</connection-url>
           <driver-class>com.jnetdirect.jsql.JSQLDriver</driver-class>
           <user-name>process-jboss</user-name>
           <password>pjss986sPROD</password>
           </local-tx-datasource>
          </datasources>
          
          persistence.xml as follows:
          
          <?xml version="1.0" encoding="UTF-8"?>
          <persistence>
           <persistence-unit name="RSDB">
           <provider>org.hibernate.ejb.HibernatePersistence</provider>
           <jta-data-source>java:jdbc/RespaJbossDS</jta-data-source>
           <properties>
           <property name="hibernate.hbm2ddl.auto" value="update"/>
           <property name="hibernate.show_sql" value="true"/>
           <!-- These are the default for JBoss EJB3, but not for HEM: -->
           <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
           <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
           </properties>
           </persistence-unit>
          </persistence>


          • 2. Re: PersistenceException during testNG unit test
            Arbi Sookazian Master

            If anyone can please answer the datasource problem, I'd greatly appreciate it.

            • 3. Re: PersistenceException during testNG unit test
              Sheldon s Newbie

              have you solved the problem? I am having the exact problem too. I'd like to know your answer. Thanks.

              Sheldon