2

我使用 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")

使用 (2) even for GreeterImpl, or GreeterLocalorGreeterRemote它给了我错误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 Embedded 和 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

注意:默认情况下部署为测试应用程序还是不作为我制作的“costa-services-depa”?

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

4

1 回答 1

2

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

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

他们说 GlassFish 中的注入在某些条件下无法部署战争:

  1. 该应用程序<absoluteOrdering>web.xml

  2. 应用程序的类全部打包为目录中的 jar 档案,[war]\WEB-INF\lib\而不是解压后包含在目​​录中[war]\WEB-INF\classes\

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

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

于 2015-05-27T11:04:34.673 回答