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

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


      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
      public void testDashboardAction()

      EntityManager em = getEntityManagerFactory().createEntityManager();

      User user = new User();

      DashboardAction action = new DashboardAction();


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

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

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

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


      private EntityManagerFactory emf;

      public EntityManagerFactory getEntityManagerFactory()
      return emf;

      public void init()
      emf = Persistence.createEntityManagerFactory("RSDB");

      public void destroy()


      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.


        • 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:

           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"?>
          persistence.xml as follows:
          <?xml version="1.0" encoding="UTF-8"?>
           <persistence-unit name="RSDB">
           <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"/>

          • 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.