首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arquillian不能在玻璃鱼嵌入式容器上注入本地或远程EJB

Arquillian不能在玻璃鱼嵌入式容器上注入本地或远程EJB
EN

Stack Overflow用户
提问于 2015-04-30 09:16:08
回答 1查看 1.2K关注 0票数 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),即使对于GreeterImpl,或者GreeterLocalGreeterRemote,它也给出了错误Could not inject members

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

这是我代码的一部分:

代码语言:javascript
复制
@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

代码语言:javascript
复制
...
<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

代码语言:javascript
复制
...
<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

代码语言:javascript
复制
...
<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

代码语言:javascript
复制
<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>

关于嵌入的玻璃鱼和Arquillian及Junit的依赖关系:

代码语言:javascript
复制
<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>

在控制台中,我注意到:

代码语言:javascript
复制
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调用的过程中,我使用了测试而不是costa-service-depa,但它总是给我带来相同的问题(NullPointerException)。

EN

回答 1

Stack Overflow用户

发布于 2015-05-27 11:04:34

我也遇到了同样的问题,试图在针对GlassFish 4.1的Arquillian测试中注入一个上下文bean。这个问题似乎与GlassFish (主要的和开放的)错误有关:

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

他们说,在特定条件下,在GlassFish中注射是行不通的。

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

发生在我身上的是我的@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被正确地注入了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29963705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档