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 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.