Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Test JPA/Hibernate : No Persistence provider for EntityManager

I want to enable Unit Test for my project. I have created the structure as follows.

Directory Structure

.
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   └── resources
│   │       ├── META-INF
│   │       │   ├── beans.xml        <-- Works fine in live
│   │       │   └── persistence.xml  <-- Works fine in live
│   └── test
│       ├── java
│       │   ├── com
│       │   │   └── test
│       │   │       └── model
│       │   │           ├── TestEntityManagerUtil.java
│       │   │           └── TestHibernate.java
│       └── resources
│           ├── META-INF
│           │   ├── beans.xml
│           │   └── persistence.xml

CODE

persistence.xml

<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">
    <persistence-unit name="test" transaction-type="RESOURCE_LOCAL">

        <description>TEST Persistence Unit</description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <properties>

            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url" value="jdbc:mysql://XXX:3306/X" />
            <property name="hibernate.connection.username" value="X" />
            <property name="hibernate.connection.password" value="X" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.transaction.flush_before_completion" value="true" />
        </properties>
    </persistence-unit>
</persistence>

TestEntityManagerUtil.java

public class TestEntityManagerUtil {
    public static EntityManagerFactory getEntityManagerFactory(){
        return Persistence.createEntityManagerFactory("test");
    }
}

TestHibernate.java

public class TestHibernate {

    private EntityManager em;

    @Before
    public void beforeEach(){
        // Exception occurs here
        em = TestEntityManagerUtil.getEntityManagerFactory().createEntityManager();
    }

    @After
    ....

    @Test
    ....
}

Exception

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)

The test persistence.xml is exploded in target/test-clases directory as it is.

Can anyone help me what i would be doing wrong.

like image 245
Talha Ahmed Khan Avatar asked Dec 26 '22 14:12

Talha Ahmed Khan


1 Answers

Please review your JPA implementation library (Hibernate) dependencies in your Maven POM.

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>${hibernate-core-version}</version>
</dependency>'

Otherwise add hibernate-entityManager.jar in a non Maven project

Edit:

Be aware that the JBoss example you are using is an EE application which uses CDI injection to obtain an EntityManager. A major misconception is to assume that a JUnit tests are operational in EE applications.... No, JUnit tests can neither run the JBoss container nor deploy your project. Yet you can refer to http://arquillian.org/invasion/ and also notice the JBoss example for an arquillian test

In other words, your JUnit is the SE way of testing an EntityManager and it in not operational in an application server like JBoss.

like image 50
Sym-Sym Avatar answered Dec 30 '22 10:12

Sym-Sym