title: Getting "No Persistence provider for EntityManager named" when Maven build is run with Junit testcases
body: <p>When running the maven build with Junit getting "No Persistence provider for EntityManager named" error.Not able to identify what is missing in my code.</p>
  
  <p>Main class:</p>
  
  <pre><code>public class ApprovalHistory {
  
      @PersistenceContext(unitName = "Approval_History")  
      private Logger logger = LoggerFactory.getLogger(ApprovalHistory.class);
  
      public EntityManagerFactory emfactory = null;
  
      final String JDBC_URL_H2DB = "jdbc:h2:file:./APApproval/ApprovalHistoryH2DB";
      final String JDBC_USERNAME_H2DB = "";
      final String JDBC_PASSWORD_H2DB = "";
  
      final String JDBC_DRIVER_SQL = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
      final String JDBC_DRIVER_H2 = "org.h2.Driver";
      final String JDBC_DRIVER_HSQL = "org.hsqldb.jdbc.JDBCDriver";
  
      final String PERSISTANCE_UNIT_NAME = "Approval_History";
  
      final String SELECT_BO_TABLE = "SELECT bobj FROM BusinessObjectTable bobj where bobj.docId =:";
      final String SELECT_COMMENT_TABLE = "SELECT comment FROM CommentTable comment where comment.actionTable.id IN :";
      final String SELECT_REASON_TABLE = "SELECT reason FROM ReasonTable reason where reason.actionTable.id IN :";    
  
      public ApprovalHistory()
      {
          try {
              createEntityManagerFactory();
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  
      public void insertData(List&lt;ApprovalHistoryModel&gt; historyList){
  
          if(historyList != null &amp;&amp; !historyList.isEmpty())
          {
              EntityManager entitymanager = null;
  
              try
              {           
                  entitymanager = getEntityManager();
                  entitymanager.getTransaction().begin(); 
  
  
                  ApprovalHistoryModel firstItem = historyList.get(0);
  
                  ActionTable a = new ActionTable(firstItem.actionType, firstItem.tenantId, firstItem.comment, firstItem.reason);
  
                  for(ApprovalHistoryModel h : historyList) 
                  {
                      a.getBusinessObjects().add(new BusinessObjectTable(h.userName, h.taskId, h.docId, h.objectIdentifier1, h.objectIdentifier2, h.objectIdentifier3,h.tenantId));
                  }
  
                  entitymanager.persist(a);
  
                  entitymanager.getTransaction().commit();                    
              }
              catch (RuntimeException e) {
                  if(entitymanager!=null &amp;&amp; entitymanager.getTransaction().isActive()) {
                      entitymanager.getTransaction().rollback();
                  }
                  throw e;
              }
  
              finally{
                  closeEntityManager(entitymanager);
              }
          }
      }
  
  
      public List&lt;ApprovalHistoryModel&gt; getApprovalHistory(String docID) throws Exception
      {
          logger.info("=ApprovalConnector=: start of getApprovalHistory()");
  
          List&lt;ApprovalHistoryModel&gt; historyModels = new ArrayList&lt;ApprovalHistoryModel&gt;();
  
          if(docID!=null &amp;&amp; !docID.isEmpty()) {
          EntityManager entitymanager = null;
  
          try
          {
              entitymanager = getEntityManager();     
  
              TypedQuery&lt;BusinessObjectTable&gt; bobjquery =  entitymanager.createQuery(SELECT_BO_TABLE+"DocID ",BusinessObjectTable.class);
              bobjquery.setParameter("DocID", docID);         
              List&lt;BusinessObjectTable&gt; bobjs = bobjquery.getResultList();    
  
              if(bobjs!=null){
                  for (BusinessObjectTable bobj : bobjs) {
                      ActionTable a = bobj.getActionTable();
  
                      ApprovalHistoryModel history = new ApprovalHistoryModel();
                      history.docId = bobj.getDocId();
                      history.taskId = bobj.getApprovalItemId();
                      history.userName = bobj.getUserName();
  
                      logger.debug("=ApprovalConnector=: getApprovalHistory(): documentID - "+bobj.getDocId());
                      history.actionType = a.getActionType();
  
                      logger.debug("=ApprovalConnector=: getApprovalHistory(): actionType - "+ history.actionType);
  
                      history.actionDate = ISODateTimeFormat.dateTime().print(new DateTime(a.getActionDate()));
                      history.objectIdentifier1 = bobj.getObjectIdentifier1();
                      history.objectIdentifier2=bobj.getObjectIdentifier2();
                      history.objectIdentifier3 = bobj.getObjectIdentifier3();
                      history.tenantId = a.getTenantId();
  
                      history.comment = a.getComment()!=null?a.getComment().getComment():"";
                      history.reason = a.getReason()!=null?a.getReason().getReason():"";
  
                      historyModels.add(history);
                  }
  
              }
              logger.info("=ApprovalConnector=: end of getApprovalHistory()");
          }
          finally{
              closeEntityManager(entitymanager);
          }   
      }
  
          return historyModels;
      }
  
      public void createEntityManagerFactory() throws Exception
      {               
          Map&lt;String, String&gt; persistenceMap = new HashMap&lt;String, String&gt;();
  
          String jdbcDriver = getJdbcDriverName(JDBC_URL_H2DB);
  
          persistenceMap.put("javax.persistence.jdbc.driver", jdbcDriver);
          persistenceMap.put("javax.persistence.jdbc.url", JDBC_URL_H2DB);
  
          if (!JDBC_USERNAME_H2DB.isEmpty()) {
              persistenceMap.put("javax.persistence.jdbc.user", JDBC_USERNAME_H2DB);
          }
          if (!JDBC_PASSWORD_H2DB.isEmpty()) {
              persistenceMap.put("javax.persistence.jdbc.password", JDBC_PASSWORD_H2DB);
          }
  
          persistenceMap.put("eclipselink.session-name",System.currentTimeMillis() + "");
  
          this.emfactory = Persistence.createEntityManagerFactory(PERSISTANCE_UNIT_NAME, persistenceMap); 
  
      }
  
      public EntityManager getEntityManager()
      {               
          EntityManager entitymanager = this.emfactory.createEntityManager();
          return entitymanager;
      }
  
      public void closeEntityManager(EntityManager entitymanager)
      {
          if(entitymanager!=null)
              entitymanager.close();      
      }
      public void closeEntityManagerFactory()
      {
          if(this.emfactory!=null)
              this.emfactory.close();
      }
  
      private String getJdbcDriverName(String jdbcUrl) {
          if (jdbcUrl.startsWith("jdbc:sqlserver"))
              return JDBC_DRIVER_SQL;
          if (jdbcUrl.startsWith("jdbc:h2"))
              return JDBC_DRIVER_H2;
          if (jdbcUrl.startsWith("jdbc:hsqldb"))
              return JDBC_DRIVER_HSQL;
          return null;
      }
  
  
  
  Test Calss:
  
      public class ApprovalHistoryTest {
  
          ApprovalHistory approvalHistory = new ApprovalHistory();
  
          @Before
          public void setUp() throws Exception {
  
              List&lt;ApprovalHistoryModel&gt; actionHistoryModels = new ArrayList&lt;ApprovalHistoryModel&gt;();     
  
                  for(int i=0;i&lt;=2;i++){
                  ApprovalHistoryModel history = new ApprovalHistoryModel();
                  String comment = "comment no. " + i;
                  String reason = "reason no. " + i;
                  String userName = "User" + i;
                  history.taskId = "321YZ61_0026CV7Z0000XB" + i;
                  history.actionDate = ISODateTimeFormat.dateTime().print(new DateTime(new Date()));
                  history.actionType = i;
                  history.comment = comment.trim();
                  history.docId = "321YZ61_026CV7Z0000TD" + i;
                  history.userName = userName;
                  history.reason = reason;
  
                  actionHistoryModels.add(history);
  
                  }   
              approvalHistory.insertData(actionHistoryModels);
          }
  
          @After
          public void tearDown() throws Exception {
              DeleteApprovalHistory history = new DeleteApprovalHistory();
              try{
              history.purgeRecord(0,"DAYS");  
              approvalHistory.closeEntityManagerFactory();
          }catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          }
  
  
          @Test()
          public void test() {
              //ApprovalHistory approvalHistory = new ApprovalHistory();
              List&lt;ApprovalHistoryModel&gt; historyList = new ArrayList&lt;ApprovalHistoryModel&gt;(); 
              for(int i=0;i&lt;=2;i++){
              ApprovalHistoryModel history = new ApprovalHistoryModel();
                try {
                    Thread.sleep(1000);
                    historyList=approvalHistory.getApprovalHistory(history.docId);
                    assertEquals("321YZ61_0026CV7Z0000XB" + i,historyList.get(i).taskId);`enter code here`
                    assertEquals("comment no. " + i,historyList.get(i).comment);
                    assertEquals("User" + i,historyList.get(i).userName);
                    assertEquals("reason no. " + i,historyList.get(i).reason);
                    assertEquals(i,historyList.get(i).actionType);
  
  
              } catch (Exception e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }   
              }
          }
  
      }
  
  Persistence.xml
  
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.1"
      xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd"&gt;
  
    &lt;persistence-unit name="Approval_History" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;class&gt;com.perceptivesoftware.apapproval.history.ActionTable&lt;/class&gt;
      &lt;class&gt;com.perceptivesoftware.apapproval.history.BusinessObjectTable&lt;/class&gt;
      &lt;class&gt;com.perceptivesoftware.apapproval.history.CommentTable&lt;/class&gt;
      &lt;class&gt;com.perceptivesoftware.apapproval.history.ReasonTable&lt;/class&gt;
      &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
       &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
      &lt;properties&gt;
        &lt;property name="eclipselink.ddl-generation" value="create-or-extend-tables" /&gt;
        &lt;property name="eclipselink.logging.level.sql" value="FINE"/&gt;
        &lt;property name="eclipselink.logging.parameters" value="true"/&gt;
        &lt;property name="eclipselink.multitenant.tenants-share-cache" value="true" /&gt;          
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>Error::</p>
  
  <blockquote>
    <p>Running ApprovalHistoryTest javax.persistence.PersistenceException: No
    Persistence provider for EntityManager named Approval_History     at
    javax.persistence.Persistence.createEntityManagerFactory(Unknown
    Source)   at
    com.perceptivesoftware.apapproval.history.ApprovalHistory.createEntityManagerFactory(ApprovalHistory.java:167)
      at
    com.perceptivesoftware.apapproval.history.ApprovalHistory.(ApprovalHistory.java:48)
      at ApprovalHistoryTest.(ApprovalHistoryTest.java:20)  at
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
      at
    org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:187)
      at
    org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:236)
      at
    org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
      at
    org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:233)
      at
    org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
      at
    org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)  at
    org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)   at
    org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)     at
    org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)   at
    org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)  at
    org.junit.runners.ParentRunner.run(ParentRunner.java:300)     at
    org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
      at
    org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
      at
    org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)     at
    org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
      at
    org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
      at
    org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
      at
    org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
      at
    org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
    javax.persistence.PersistenceException: No Persistence provider for
    EntityManager named Approval_History  at
    javax.persistence.Persistence.createEntityManagerFactory(Unknown
    Source)   at
    com.perceptivesoftware.apapproval.history.ApprovalHistory.createEntityManagerFactory(ApprovalHistory.java:167)
      at
    com.perceptivesoftware.apapproval.history.ApprovalHistory.(ApprovalHistory.java:48)
      at
    com.perceptivesoftware.apapproval.history.DeleteApprovalHistory.purgeRecord(DeleteApprovalHistory.java:46)
      at ApprovalHistoryTest.tearDown(ApprovalHistoryTest.java:50)    at
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)     at
    org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
      at
    org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
      at
    org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
      at
    org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)    at
    org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
      at
    org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)  at
    org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)   at
    org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)     at
    org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)   at
    org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)  at
    org.junit.runners.ParentRunner.run(ParentRunner.java:300)     at
    org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
      at
    org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
      at
    org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)     at
    org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
      at
    org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
      at
    org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
      at
    org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
      at
    org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)</p>
  </blockquote>