Junit 阿奎利安罐头';t在Glassfish嵌入式容器上注入本地或远程EJB

Junit 阿奎利安罐头';t在Glassfish嵌入式容器上注入本地或远程EJB,junit,junit4,jboss-arquillian,glassfish-embedded,arquillian-drone,Junit,Junit4,Jboss Arquillian,Glassfish Embedded,Arquillian Drone,我使用Arquillian测试具有显式本地和远程接口的EJB。但在测试中,Arquillian不会在具有本地接口或远程接口类型的字段中“注入”任何内容 我使用嵌入的Glassfish作为测试服务器,并使用junit4 我用过: @EJB @Inject @EJB(lookup=“java:global/costa-services-depa/GreeterImpl!es.costa.GreeterLocal”) @EJB(lookup=“java:global/costa-services-de

我使用Arquillian测试具有显式本地和远程接口的EJB。但在测试中,Arquillian不会在具有本地接口或远程接口类型的字段中“注入”任何内容

我使用嵌入的Glassfish作为测试服务器,并使用junit4

我用过:

  • @EJB
  • @Inject
  • @EJB(lookup=“java:global/costa-services-depa/GreeterImpl!es.costa.GreeterLocal”)
  • @EJB(lookup=“java:global/costa-services-depa/GreeterImpl!es.costa.greetremote”)
  • 使用(2)即使对于
    GreeterImpl
    ,或
    GreeterLocal
    GreeterRemote
    也会出现错误
    无法插入成员

    使用(1,3,4),我得到一个
    java.lang.NullPointerException
    ,这意味着EJB没有被注入

    这是我代码的一部分:

    @RunWith(Arquillian.class)
    public class greeterTest {
    @Deployment
    public static Archive<?> createDeployment() {
        JavaArchive jar = ShrinkWrap.create(JavaArchive.class,"costa-services-depa")
            .addClasses(
                    GreeterRemote.class,
                    GreeterLocal.class,
                    Greeter.class)
            .addAsManifestResource("test-persistence.xml", "persistence.xml")
            .addAsManifestResource("jbossas-ds.xml")
            .addAsManifestResource("META-INF/beans.xml",
                    ArchivePaths.create("beans.xml"));
        return jar;
    }
    
    @Inject
    GreeterImpl greeter;
    
    
    @Test
    public void testTestServiceLocal(){
        System.out.println(greeter.getMessage());
    } 
    }
    
    以下是
    测试persistence.xml

    ...
    <resources>
    <jdbc-resource pool-name="ArquillianEmbeddedH2Pool"
        jndi-name="jdbc/arquillian"/>
    <jdbc-connection-pool name="ArquillianEmbeddedH2Pool"
        res-type="javax.sql.DataSource"
        datasource-classname="org.h2.jdbcx.JdbcDataSource">
        <property name="user" value="sa"/>
        <property name="password" value=""/>
        <property name="url" value="jdbc:h2:file:target/databases/h2/db"/>
    </jdbc-connection-pool>
    </resources>
    
    ...
    <persistence-unit name="test">
        <jta-data-source>jdbc/arquillian</jta-data-source>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.logging.level.sql" value="FINE"/>
            <property name="eclipselink.logging.parameters" value="true"/>
        </properties>
    </persistence-unit>
    
    ...
    <container qualifier="glassfish-embedded" default="true">
        <configuration>
            <property name="resourcesXml">
                ejbModule/src/test/resources-glassfish-embedded/glassfish-resources.xml
            </property>
        </configuration>
    </container>
    ...
    
    <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>
    
    这是jbossas ds.xml:

    ...
    <resources>
    <jdbc-resource pool-name="ArquillianEmbeddedH2Pool"
        jndi-name="jdbc/arquillian"/>
    <jdbc-connection-pool name="ArquillianEmbeddedH2Pool"
        res-type="javax.sql.DataSource"
        datasource-classname="org.h2.jdbcx.JdbcDataSource">
        <property name="user" value="sa"/>
        <property name="password" value=""/>
        <property name="url" value="jdbc:h2:file:target/databases/h2/db"/>
    </jdbc-connection-pool>
    </resources>
    
    ...
    <persistence-unit name="test">
        <jta-data-source>jdbc/arquillian</jta-data-source>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.logging.level.sql" value="FINE"/>
            <property name="eclipselink.logging.parameters" value="true"/>
        </properties>
    </persistence-unit>
    
    ...
    <container qualifier="glassfish-embedded" default="true">
        <configuration>
            <property name="resourcesXml">
                ejbModule/src/test/resources-glassfish-embedded/glassfish-resources.xml
            </property>
        </configuration>
    </container>
    ...
    
    <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>
    
    注意:默认情况下,部署为测试应用程序,还是不部署为我制作的“costa services depa”


    要知道,在EJB的调用路径中,我放置了test而不是costa服务depa,但它总是给我带来相同的问题(
    NullPointerException
    )。

    我在针对GlassFish 4.1的Arquillian测试中尝试注入上下文bean时遇到了相同的问题。 此问题似乎与GlassFish(主要和开放)错误有关:

    他们说在玻璃鱼身上注射疫苗在特定条件下部署战争是行不通的:

  • 应用程序在
    web.xml

  • 应用程序的类都作为jar归档打包在
    [war]\WEB-INF\lib\
    目录中,而不是包含在
    [war]\WEB-INF\classes\
    目录中

  • 发生在我身上的是,我的
    @Singleton
    bean被创建和注入,但是对于应用程序中的每个
    @EJB
    引用,不仅仅是创建一次。 我检查了我在GF上的Arquillian部署(
    glassfish-4.1\glassfish\domains\domain1\applications\test
    ),我可以看到部署在
    [war]\WEB-INF\lib\
    中的
    test.jar
    。我的应用程序中没有
    web.xml
    ,我在GlassFish部署中也没有看到它。我猜
    web.xml
    是自动提供的


    在Wildfly上使用Arquillian部署相同的应用程序效果很好,我的
    @Singleton
    bean按预期正确注入。

    抱歉,我对Glassfish(精确嵌入)没有太多经验,问题看起来很奇怪。根据描述,你做得很好。但对于实验,您能否切换到另一种类型的应用程序服务器并再次进行测试?WildFly/JBoss将是首选。抱歉,JBoss管理下,它运行良好,但该公司希望使用glassfish embeddedTry!我的经验告诉我,JBoss/WildFly可以向您展示一些隐藏的问题,让您重新审视这个问题。