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
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.
source share