Enterprise Java Development@TOPIC@

Chapter 90. IT Testing

90.1. EJB Access
90.1.1. JNDI
90.1.2. JBoss Remoting
90.1.3. EJB Client
90.1.4. Blending JBoss Remoting and EJB Client
90.2. Integration Test Session EJB
90.2.1. Integration Test (IT) JUnit Class
90.2.2. @Test Methods
90.3. IT Module
90.4. IT Test Execution
90.5. Summary
  • InitialContextFactory looks for custom naming extensions when encountering naming prefix ("ejb:")

  • java.naming.factory.url.pkgs lists base package names to start looking for extensions

  • jboss-ejb-client.jar must be in the classpath

  • Provider extracts "ejb" prefix from "ejb:/..." JNDI name

    ejb:/ejb-basic-ejb/GreeterEJB!info.ejava.examples.ejb.basic.ejb.GreeterRemote
  • Provider obtains list of Java package prefixes from "jndi.naming.factory.url.pkgs"

    #jndi.properties
    java.naming.factory.url.pkgs=org.jboss.ejb.client.naming
    
  • Provider searches for "org.jboss.ejb.client.naming.ejb" Java package in classpath

    $ mvn dependency:tree
    ...
    [INFO] +- info.ejava.examples.common:jboss-rmi-client:pom:5.0.0-SNAPSHOT:test
    [INFO] |  |  +- org.jboss:jboss-ejb-client:jar:4.0.10.Final:test
    [INFO] |  |  +- org.jboss.remoting:jboss-remoting:jar:5.0.5.Final:test
    [INFO] |  |  +- org.wildfly:wildfly-naming-client:jar:1.0.9.Final:test
    [INFO] |  |  +- org.wildfly.wildfly-http-client:wildfly-http-naming-client:jar:1.0.12.Final:test
    ...
    
  • Provider locates an ObjectFactory class in the "org.jboss.ejb.client.naming" Java package that starts with "ejb"

    $ jar tf ~/.m2/repository/org/jboss/jboss-ejb-client/4.0.10.Final/jboss-ejb-client-4.0.10.Final.jar | grep org.jboss.ejb.client.naming
    org/jboss/ejb/client/naming/ejb/ejbURLContextFactory.class
    
  • Provider uses that ObjectFactory to resolve the remaining portion of the JNDI name

    ejb:/ejb-basic-ejb/GreeterEJB!info.ejava.examples.ejb.basic.ejb.GreeterRemote
...

import static org.junit.Assert.*;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import info.ejava.examples.ejb.basic.dto.Greeting;
import info.ejava.examples.ejb.basic.dto.Name;
import info.ejava.examples.ejb.basic.ejb.BadRequestException;
import info.ejava.examples.ejb.basic.ejb.GreeterRemote;
...
public class GreeterIT {
    private static final Logger logger = LoggerFactory.getLogger(GreeterBase.class);
    protected Properties jndiProperties; // varies whether using Remoting or EJBClient
    protected String jndiName; // varies whether accessing EJB, WAR or EAR deployment and access
    protected Context jndi;
    protected GreeterRemote greeter;
    @Before
    public void setUp() throws Exception {
        jndi = new InitialContext(jndiProperties);
        greeter = (GreeterRemote) jndi.lookup(jndiName);
    }   
    @After
    public void tearDown() throws Exception {
        if (jndi != null) {
            jndi.close(); // produces errors with JBoss Remoting
        }   
    }   
.
|-- pom.xml
`-- src
    `-- test
        |-- java
        |   `-- info
        |       `-- ejava
        |           `-- examples
        |               `-- ejb
        |                   `-- basicejb
        |                       `-- ejbclient
        |                           |-- GreeterIT.java
        `-- resources
            |-- jndi.properties
            `-- log4j.xml
$ mvn clean verify
...
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ ejb-basic-test ---
[INFO] Deleting .../ejb-basic-example/ejb-basic-test/target
...
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ ejb-basic-test ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ejb-basic-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ ejb-basic-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 5 source files to .../ejb-basic-example/ejb-basic-test/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ ejb-basic-test ---
[INFO] 
[INFO] --- maven-jar-plugin:2.5:jar (default-jar) @ ejb-basic-test ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: .../ejb-basic-example/ejb-basic-test/target/ejb-basic-test-4.0.0-SNAPSHOT.jar
...

[INFO] --- cargo-maven2-plugin:1.4.3:redeploy (cargo-prep) @ ejb-basic-test ---
Sep 28, 2014 11:58:23 PM org.xnio.Xnio <clinit>
INFO: XNIO version 3.2.2.Final
Sep 28, 2014 11:58:23 PM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.2.2.Final
Sep 28, 2014 11:58:23 PM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 4.0.3.Final

...
[INFO] --- maven-failsafe-plugin:2.17:integration-test (integration-tests) @ ejb-basic-test ---
[INFO] Failsafe report directory: .../ejb-basic-example/ejb-basic-test/target/failsafe-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Running info.ejava.examples.ejb.basicejb.ejbclient.GreeterRemotingWARIT
23:58:26,441 INFO  (GreeterIT.java:43) -using jndiName=ejb-basic-war/GreeterEJB!info.ejava.examples.ejb.basic.ejb.GreeterRemote
...
23:58:27,811 INFO  (GreeterIT.java:43) -using jndiName=ejb:ejb-basic-ear/ejb-basic-ejb/GreeterEJB!info.ejava.examples.ejb.basic.ejb.GreeterRemote
...
23:58:28,167 INFO  (GreeterIT.java:43) -using jndiName=ejb-basic-ear/ejb-basic-ejb/GreeterEJB!info.ejava.examples.ejb.basic.ejb.GreeterRemote
...
23:58:28,289 INFO  (GreeterIT.java:43) -using jndiName=ejb:/ejb-basic-war/GreeterEJB!info.ejava.examples.ejb.basic.ejb.GreeterRemote

Results :

Tests run: 12, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- cargo-maven2-plugin:1.4.3:undeploy (cargo-post) @ ejb-basic-test ---
[INFO] 
[INFO] --- maven-failsafe-plugin:2.17:verify (verify) @ ejb-basic-test ---
[INFO] Failsafe report directory: .../ejb-basic-example/ejb-basic-test/target/failsafe-reports
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.036 s