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">
            <!-- To test against OpenEJB -->
                <!-- necessary in combination with Hibernate -->
                <!-- necessary in combination with Hibernate-->
  • 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;
        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.setName("Master Suite");
                Cabin cabin_2 = dao.findCabin(1);
            } catch (javax.naming.NamingException ne) {
        public static Context getInitialContext()
                throws javax.naming.NamingException {
            InitialContext context = null;
            Properties props = new Properties();
            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.

About Pascal Alma

Pascal is a senior software developer and architect. Pascal has been designing and building applications since 2001. He is particularly interested in Open Source toolstack (Mule, Spring Framework, JBoss) and technologies like Web Services, SOA and Cloud technologies. Lately he is having great fun by building iOS apps with Swift. Specialties: JEE AWS XML/XSD/XSLT Web Services/SOA Mule ESB/ WSO2 ESB Maven Cloud Technology Swift/ iOS
This entry was posted in EJB3, JBoss, Maven and tagged , , , . Bookmark the permalink.