Abder KRIMA
Abder KRIMA

Reputation: 3678

Arquillian can't inject a local or remote EJB on Glassfish embedded container

I use Arquillian to test an EJB that has an explicit local and remote interface. But in the test Arquillian does not "inject" anything in a field that has the type of the local interface, or the remote interface.

I use the Glassfish embedded as server for test and I use junit4.

I used :

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

With (2) even for GreeterImpl, or GreeterLocal or GreeterRemote it gives me the error Could not inject members.

With (1,3,4) I get a java.lang.NullPointerException which means that the EJB is not injected.

This is a part of my code:

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

Here is the 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>

Here is the 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>

This is the arquillian.xml:

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

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

About dependencies for Glassfish embedded and Arquillian and 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>

In the console I noticed:

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

NB : By default that deploys as test application or not as what I made "costa-services-depa"?

To know that in the path of call of EJB I put test instead of costa-service-depa but always it gives me the same problem (NullPointerException).

Upvotes: 2

Views: 1221

Answers (1)

Razvan Vacaru
Razvan Vacaru

Reputation: 99

I'm having the same problem trying to somehow inject a context bean in an Arquillian test against GlassFish 4.1. This issue seems related to a GlassFish (major and open) bug:

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

They say injections in GlassFish won't work deploying a war with certain conditions:

  1. The application uses an empty <absoluteOrdering> tag in web.xml

  2. The classes of the application are all packed as jar archives in the [war]\WEB-INF\lib\ directory, instead of included them unpacked in the [war]\WEB-INF\classes\ directory

What happens to me is that my @Singleton bean is created and injected but for every @EJB reference in the app, not just created once. I checked my Arquillian deployment on GF (glassfish-4.1\glassfish\domains\domain1\applications\test) and I can see the test.jar deployed in [war]\WEB-INF\lib\. I don't have a web.xml in my app and I don't see it in my GlassFish deployment. I guess the web.xml is provided somehow automatically.

Deploying the same app with Arquillian on Wildfly works fine and my @Singleton bean is injected correctly as expected.

Upvotes: 2

Related Questions