Arquillian无法在Glassfish嵌入式容器

时间:2015-04-30 09:16:09

标签: junit junit4 jboss-arquillian glassfish-embedded arquillian-drone

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

我使用Glassfish作为服务器进行测试,我使用junit4。

我用过:

  1. @EJB
  2. @Inject
  3. @EJB(lookup="java:global/costa-services-depa/GreeterImpl!es.costa.GreeterLocal")
  4. @EJB(lookup="java:global/costa-services-depa/GreeterImpl!es.costa.GreeterRemote")
  5. 即使对于GreeterImpl,或GreeterLocalGreeterRemote,它也会给我错误Could not inject members

    使用(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());
    } 
    }
    

    以下是glassfish-resources.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>
    

    以下是test-persistence.xml

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

    这是arquillian.xml

    ...
    <container qualifier="glassfish-embedded" default="true">
        <configuration>
            <property name="resourcesXml">
                ejbModule/src/test/resources-glassfish-embedded/glassfish-resources.xml
            </property>
        </configuration>
    </container>
    ...
    

    这是jbossas-ds.xml

    <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>
    

    关于Glassfish嵌入式和Arquillian和Junit的依赖关系:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.arquillian</groupId>
                <artifactId>arquillian-bom</artifactId>
                <version>1.0.3.Final</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <scope>test</scope>
        </dependency>
    
     <profiles>
        <profile>
            <id>arquillian-glassfish-embedded</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.jboss.arquillian.container</groupId>
                    <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
                    <version>1.0.0.CR3</version>
                    <scope>test</scope>
                </dependency>
                <dependency>
                    <groupId>org.glassfish.main.extras</groupId>
                    <artifactId>glassfish-embedded-all</artifactId>
                    <version>3.1.2.2</version>
                    <scope>provided</scope>
                </dependency>
                <dependency>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                    <version>1.3.166</version>
                    <scope>test</scope>
                </dependency>
            </dependencies>
    
            <build>
                <testResources>
                    <testResource>
                        <directory>ejbModule/src/test/resources</directory>
                    </testResource>
                    <testResource>
                        <directory>ejbModule/src/test/resources-glassfish-embedded</directory>
                    </testResource>
                </testResources>
                <plugins>
                    <plugin>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.12</version>
                        <configuration>
                            <systemPropertyVariables>
                                <arquillian.launch>glassfish-embedded</arquillian.launch>
                                <java.util.logging.config.file>
                                    ${project.build.testOutputDirectory}/logging.properties
                                </java.util.logging.config.file>
                                <derby.stream.error.file>
                                    ${project.build.directory}/derby.log
                                </derby.stream.error.file>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    在控制台中我注意到了:

    Infos: Network listener https-listener on port 0 disabled per domain.xml
    Infos: Grizzly Framework 1.9.50 started in: 32ms - bound to [0.0.0.0:8181]
    Infos: GlassFish Server Open Source Edition 3.1.2.2 (java_re) startup time : 
    Embedded (525ms), startup services(384ms), total(909ms)
    Infos: command add-resources result: 
    PlainTextActionReporterSUCCESSDescription: add-resources AdminCommandnull
    JDBC connection pool ArquillianEmbeddedH2Pool created successfully.
    JDBC resource jdbc/arquillian created successfully.
    Infos: SEC1002: Security Manager is OFF.
    Infos: SEC1010: Entering Security Startup Service
    Infos: SEC1143: Loading policy provider 
    com.sun.enterprise.security.jacc.provider.SimplePolicyProvider.
    Infos: SEC1115: Realm [admin-realm] of classtype 
    [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
    [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
    Infos: SEC1115: Realm [file] of classtype 
    [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
    Infos: SEC1115: Realm [certificate] of classtype 
    [com.sun.enterprise.security.auth.realm.certificate.CertificateRealm] successfully created.
    Infos: SEC1011: Security Service(s) Started Successfully
    Infos: WEB0169: Created HTTP listener [http-listener] on host/port [0.0.0.0:8181]
    Infos: WEB0171: Created virtual server [server]
    Infos: WEB0172: Virtual server [server] loaded default web module []
    Infos: WELD-000900 SNAPSHOT
    Infos: WEB0671: Loading application [**test**] at [**/test**]
    Infos: test was successfully deployed in 1 822 milliseconds.
    PlainTextActionReporterSUCCESSNo monitoring data to report.
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.308 sec
    Infos: Shutdown procedure finished
    Infos: [Thread[GlassFish Kernel Main Thread,5,main]] exiting
    

    注意:默认情况下,部署为测试应用程序或不是我所做的&#34; costa-services-depa&#34;?

    要知道在EJB的调用路径中,我将测试放在costa-service-depa而不是costa-service-depa,但它总是给我同样的问题(NullPointerException)。

1 个答案:

答案 0 :(得分:2)

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

https://java.net/jira/browse/GLASSFISH-21167

他们说GlassFish中的注射不适用于部署具有某些条件的战争:

  1. 该应用程序在<absoluteOrdering>

  2. 中使用空web.xml标记
  3. 应用程序的类都打包为[war]\WEB-INF\lib\目录中的jar存档,而不是将它们解压缩到[war]\WEB-INF\classes\目录

  4. 我发生的事情是我的@Singleton bean已创建并注入,但应用程序中的每个 @EJB引用,而不是仅创建一次。 我在GF(glassfish-4.1\glassfish\domains\domain1\applications\test)上检查了我的Arquillian部署,我可以看到部署在test.jar中的[war]\WEB-INF\lib\。我的应用中没有web.xml,我在GlassFish部署中看不到它。我想web.xml会以某种方式自动提供。

    在Wildfly上使用Arquillian部署相同的应用程序工作正常,我的@Singleton bean按预期正确注入。