Unit Test JPA / Hibernate: Continuous Support Provider for EntityManager

I want to enable Unit Test for my project. I 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 persistence.xml test exploded in the target/test-clases directory as it is.

Can someone help me what I will do wrong.

+4
source share
1 answer

View the dependencies of your JPA (Hibernate) library in your Maven MOM.

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

Otherwise add hibernate-entityManager.jar to the project without Maven

Edit:

Keep in mind that the JBoss example you are using is an EE application that uses CDI injection to get EntityManager. The main misconception is to assume that JUnit tests work in EE applications .... No, JUnit tests cannot run the JBoss container and not deploy your project. However, you can refer to http://arquillian.org/invasion/ and also notice a JBoss example for the arquillian test

In other words, your JUnit is a way to test EntityManager, and it does not work on an application server such as JBoss.

+9
source

Source: https://habr.com/ru/post/1447170/


All Articles