[英]Arquillian Injected EntityManager is null
Using Wildfly 10 and Arquillian, injecting an EntityManager always returns NULL
.使用 Wildfly 10 和 Arquillian,注入 EntityManager 总是返回
NULL
。 Why is that?这是为什么?
Here's my test Class:这是我的测试类:
@RunWith(Arquillian.class)
public class XXXDAOTest {
@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClass(XXXDAO.class)
.addAsResource("test-ds.xml")
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Test
@PersistenceContext
public void testCreateUser(EntityManager em) {
Assert.assertNotNull("em is NULL", em);
The test fails because EntityManager
is NULL
.测试失败,因为
EntityManager
为NULL
。
The test-persistence.xml
is as follows test-persistence.xml
如下
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="xxx-ejb">
<jta-data-source>jdbc/arquillian</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
In addition I'm using a datasource declared in test-ds.xml
另外我正在使用
test-ds.xml
声明的数据源
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="xxx-ejb">
<jta-data-source>jdbc/arquillian</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
In my pom.xml
Arquillian is configured as follows:在我的
pom.xml
Arquillian 配置如下:
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
<version>1.0.0.CR8</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-spi</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-persistence-integration-tests</artifactId>
<version>1.0.0.Alpha7</version>
<scope>test</scope>
</dependency>
I've read somewhere that arquillian-weld-ee-embedded
as provided in the pom.xml
does not support persistence testing, so I added extension below.我在某处读到
pom.xml
中提供的arquillian-weld-ee-embedded
不支持持久性测试,所以我在下面添加了扩展。 I could not find any other profile for arquillian for persistence testing.我找不到用于持久性测试的 arquillian 的任何其他配置文件。
Do you have any hints why injecting the EntityManager
does not work?你有什么提示为什么注入
EntityManager
不起作用吗?
You have to define the datasource used on the container in test-ds.xml, for instance:您必须在 test-ds.xml 中定义容器上使用的数据源,例如:
<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.jboss.org/ironjacamar/schema
http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
<datasource enabled="true" jndi-name="jdbc/arquillian"
pool-name="ArquillianEmbeddedH2Pool">
<connection-url>jdbc:h2:mem:arquillian;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
</datasource>
</datasources>
The following datasource works with WildFly since the database h2 is included in the WildFly distribution.以下数据源适用于 WildFly,因为数据库 h2 包含在 WildFly 发行版中。
For a complete example see https://arquillian.org/guides/testing_java_persistence/有关完整示例,请参阅https://arquillian.org/guides/testing_java_persistence/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.