简体   繁体   English

Arquillian 注入的 EntityManager 为空

[英]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 .测试失败,因为EntityManagerNULL

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM