title: Unit Test JPA/Hibernate : No Persistence provider for EntityManager
body: <p>I want to enable Unit Test for my project. I have created the structure as follows.</p>
  
  <p>Directory Structure</p>
  
  <pre><code>.
  ├── pom.xml
  ├── src
  │   ├── main
  │   │   ├── java
  │   │   └── resources
  │   │       ├── META-INF
  │   │       │   ├── beans.xml        &lt;-- Works fine in live
  │   │       │   └── persistence.xml  &lt;-- Works fine in live
  │   └── test
  │       ├── java
  │       │   ├── com
  │       │   │   └── test
  │       │   │       └── model
  │       │   │           ├── TestEntityManagerUtil.java
  │       │   │           └── TestHibernate.java
  │       └── resources
  │           ├── META-INF
  │           │   ├── beans.xml
  │           │   └── persistence.xml
  </code></pre>
  
  <p><strong>CODE</strong></p>
  
  <p><strong>persistence.xml</strong></p>
  
  <pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
      &lt;persistence-unit name="test" transaction-type="RESOURCE_LOCAL"&gt;
  
          &lt;description&gt;TEST Persistence Unit&lt;/description&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
  
          &lt;properties&gt;
  
              &lt;property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /&gt;
              &lt;property name="hibernate.connection.url" value="jdbc:mysql://XXX:3306/X" /&gt;
              &lt;property name="hibernate.connection.username" value="X" /&gt;
              &lt;property name="hibernate.connection.password" value="X" /&gt;
  
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;
              &lt;property name="hibernate.show_sql" value="true" /&gt;
              &lt;property name="hibernate.format_sql" value="true" /&gt;
              &lt;property name="hibernate.transaction.flush_before_completion" value="true" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>TestEntityManagerUtil.java</strong></p>
  
  <pre><code>public class TestEntityManagerUtil {
      public static EntityManagerFactory getEntityManagerFactory(){
          return Persistence.createEntityManagerFactory("test");
      }
  }
  </code></pre>
  
  <p><strong>TestHibernate.java</strong></p>
  
  <pre><code>public class TestHibernate {
  
      private EntityManager em;
  
      @Before
      public void beforeEach(){
          // Exception occurs here
          em = TestEntityManagerUtil.getEntityManagerFactory().createEntityManager();
      }
  
      @After
      ....
  
      @Test
      ....
  }
  </code></pre>
  
  <p><strong>Exception</strong></p>
  
  <pre><code>javax.persistence.PersistenceException: No Persistence provider for EntityManager named test
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
      at com.wellclub.model.TestEntityManagerUtil.getEntityManagerFactory(TestEntityManagerUtil.java:12)
      at com.wellclub.model.TestHibernate.beforeEach(TestHibernate.java:24)
  </code></pre>
  
  <p>The test persistence.xml is exploded in <code>target/test-clases</code> directory as it is.</p>
  
  <p>Can anyone help me what i would be doing wrong.</p>