EJB3 with JBoss5 and Maven2 (part 3)

As I promised here this is the last part about using Maven2 to build, test and deploy an EJB3 application to JBoss5. In part 2 I showed how you can build and deploy your EJB3 application in JBoss. In this step I want to show you how you can (unit)test your EJB3 module before you deploy it to JBoss. In part 2 we had to deploy the bean first, before we could test it, but that is not the most optimal way. It would be nicer if the code is tested before it is deployed to the JBoss server. To do this, I make use of OpenEJB. This is a lightweight container that is very easy to use with Maven. Another advantage of this approach is that the EJB component is tested/used in two different EJB containers and reduces the possiblity that container-specific code is used in the application.
Although I have described a similar situation before in this post I will show the steps to get to this setup but now based on the situation of the last post. These are the steps to perform:

  • Modify the pom
  • Because we are not testing against JBoss anymore we don’t need all the dependencies in the pom anymore, only the dependency for OpenEJB and the ones to use Hibernate. This makes the pom of the ejb module look like:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>net.pascalalma.jboss</groupId>
        <artifactId>ch4</artifactId>
        <packaging>ejb</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>ch4</name>
        <url>http://maven.apache.org</url>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.4</version>
                <scope>test</scope>
            </dependency>
    
            <!-- To test against OpenEJB -->
            <dependency>
                <groupId>org.apache.openejb</groupId>
                <artifactId>openejb-core</artifactId>
                <version>3.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>3.4.0.GA</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate</artifactId>
                <version>3.2.6.ga</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>3.2.1</version>
                <scope>test</scope>
                <!-- necessary in combination with Hibernate -->
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.5.2</version>
                <scope>test</scope>
                <!-- necessary in combination with Hibernate-->
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.0.2</version>
                    <configuration>
                        <source>1.5</source>
                        <target>1.5</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ejb-plugin</artifactId>
                    <configuration>
                        <generateClient>true</generateClient>
                        <ejbVersion>3.0</ejbVersion>
                    </configuration>
                </plugin>
            </plugins>
            <finalName>${artifactId}</finalName>
        </build>
    </project>
    
  • Add the ejb-jar.xml
    To have OpenEJB recognize our module as EJB3 module we have to add an (empty) ejb-jar.xml to our project. Here is the project layout:
  • Rename the test class
  • Because I didn’t want the test class to be picked up by the Maven I didn’t put the phrase ‘Test’ in it. Now we want the test class now to be picked up, so we have to rename the class so the name contains the phrase ‘Test’. I renamed the test class to ‘ClientTest’.

  • Tune the test class
  • Besides renaming the test class we also have to make some minor changes in it. We have to modify the intial context that is returned, to have it use the OpenEJB context. And OpenEJB uses another JNDI name construction by default so I changed the used name too in the class. Here is the new version:

    package com.titan.clients;
    
    import com.titan.travelagent.TravelAgentRemote;
    import com.titan.domain.Cabin;
    
    import java.util.Properties;
    import javax.naming.Context;
    
    import javax.naming.InitialContext;
    import org.junit.Test;
    
    public class ClientTest {
    
        private static Context jndiContext = null;
    
        @Test
        public void main() {
            try {
                jndiContext = getInitialContext();
    
                //  String jndiName = "TravelAgentBean/remote";
                String jndiName = "TravelAgentBeanRemote";
                Object ref = jndiContext.lookup(jndiName);
    
                TravelAgentRemote dao = (TravelAgentRemote) ref;
    
                Cabin cabin_1 = new Cabin();
                cabin_1.setId(1);
                cabin_1.setName("Master Suite");
                cabin_1.setDeckLevel(1);
                cabin_1.setShipId(1);
                cabin_1.setBedCount(3);
    
                dao.createCabin(cabin_1);
    
                Cabin cabin_2 = dao.findCabin(1);
                System.out.println(cabin_2.getName());
                System.out.println(cabin_2.getDeckLevel());
                System.out.println(cabin_2.getShipId());
                System.out.println(cabin_2.getBedCount());
    
            } catch (javax.naming.NamingException ne) {
                ne.printStackTrace();
            }
        }
    
        public static Context getInitialContext()
                throws javax.naming.NamingException {
            InitialContext context = null;
            Properties props = new Properties();
            props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                    "org.apache.openejb.client.LocalInitialContextFactory");
            context = new InitialContext(props);
    
            return context;
        }
    }
    
  • Run it!
  • When all this is in place you can build the module and the unit test is executed in OpenEJB before it is deployed to JBoss

Now this concludes this serie of posts about testing and deploying EJB3 modules with Maven against JBoss5. I hope it may help you in some way.

Advertisement

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in EJB3, JBoss, Maven and tagged , , , . Bookmark the permalink.