プロジェクトの単体テストを有効にしたい。次のような構造を作成しました。
ディレクトリ構造
.
├── 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
コード
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
....
}
例外
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)
テストpersistence.xmlは、target/test-clases
ディレクトリ内でそのまま展開されます。
誰かが私が間違っていることを私に助けてもらえますか?