What is the best way to unit test a EJB3 component without having to deploy the component

Bruno Ranschaert picture Bruno Ranschaert · Feb 16, 2009 · Viewed 8.6k times · Source

I would like to have a JUnit (or other) system where the enterprise beans can be tested in a mock environment so that all resources/beans and so on are injected locally. The enterprise beans should not have to accomplish this. All kinds of injection should be supported.

  • I would like to have a maven plugin for this so that the tests can be run from a maven build.
  • Transactions are not necessary during the unit tests,this would require a complete container.

Do you know of such a maven plugin or test framework? Experiences with it?

Answer

toolkit picture toolkit · Feb 16, 2009

Not necessarily the easiest way, but I managed to get this working using JBoss's embeddable EJB3.0 container, with major assistance from Java Persistence with Hibernate

For a start, I don't know of maven plugins/dependencies for this, so I had to install my own locally:

(1) Download jboss-EJB-3.0_Embeddable_ALPHA_9.zip from downloads

(2) This zipfile contains two important JARs:

  • jboss-ejb3-all.jar
  • thirdparty-all.jar

(3) Install these JARs into your local repository (use your own initials instead of nkl):

mvn install:install-file \
    -Dfile=jboss-ejb3-all.jar \
    -DgroupId=nkl.jboss.embedded \
    -DartifactId=jboss-ejb3-all \
    -Dversion=3.0-alpha-9

mvn install:install-file \
    -Dfile=thirdparty-all.jar \
    -DgroupId=nkl.jboss.embedded \
    -DartifactId=thirdparty-all \
    -Dversion=3.0-alpha-9

(4) If all has gone well, you should now have two directories in your local repository.

(5) Now add the following dependencies to your pom.xml

    <dependency>
      <groupId>nkl.jboss.embedded</groupId>
      <artifactId>jboss-ejb3-all</artifactId>
      <version>3.0-alpha-9</version>
    </dependency>
    <dependency>
      <groupId>nkl.jboss.embedded</groupId>
      <artifactId>thirdparty-all</artifactId>
      <version>3.0-alpha-9</version>
    </dependency>

(6) I also needed to add dependencies for HSQLDB, and SLF4J

(7) You need to copy some files from the conf directory of the zipfile to the src/main/resources directory of your project:

  • default.persistence.properties
  • ejb3-interceptors-aop.xml
  • embedded-jboss-beans.xml
  • jndi.properties

(8) I created my own beans.xml and persistence.xml files in src/main/resources/META-INF:

<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_1_0.xsd" xmlns="urn:jboss:bean-deployer">
  <bean name="messageDSBootstrap" class="org.jboss.resource.adapter.jdbc.local.LocalTxDataSource">
    <property name="jndiName">java:/messageDS</property>
    <property name="driverClass">org.hsqldb.jdbcDriver
    </property>
    <property name="connectionURL">jdbc:hsqldb:test</property>
    <property name="minSize">0</property>
    <property name="maxSize">10</property>
    <property name="blockingTimeout">1000</property>
    <property name="idleTimeout">30000</property>
    <property name="transactionManager">
      <inject bean="TransactionManager" />
    </property>
    <property name="cachedConnectionManager">
      <inject bean="CachedConnectionManager" />
    </property>
    <property name="initialContextProperties">
      <inject bean="InitialContextProperties" />
    </property>
  </bean>
  <bean name="messageDS" class="java.lang.Object">
    <constructor factoryMethod="getDatasource">
      <factory bean="messageDSBootstrap" />
    </constructor>
  </bean>
</deployment>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
  http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
  version="1.0">
  <persistence-unit name="jpa">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/messageDS</jta-data-source>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.format_sql" value="true" />
      <property name="hibernate.hbm2ddl.auto" value="create" />
    </properties>
  </persistence-unit>
</persistence>

(9) I created a unit test like the following, using additional infomation from here.

package org.nkl;

import java.io.File;

import javax.naming.InitialContext;

import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
import org.jboss.ejb3.embedded.EJB3StandaloneDeployer;
import org.junit.Test;
import org.nkl.model.MessageHandler;

public class BasicTest {

    @Test
    public void testEjb() throws Exception {
        EJB3StandaloneBootstrap.boot(null);
        EJB3StandaloneBootstrap.deployXmlResource("META-INF/beans.xml");
        EJB3StandaloneDeployer deployer = EJB3StandaloneBootstrap
                .createDeployer();

        deployer.getArchives().add(new File("target/classes").toURI().toURL());

        deployer.create();
        deployer.start();
        InitialContext context = new InitialContext();
        MessageHandler messageHandler = (MessageHandler) context
                .lookup("MessageHandlerBean/local");
        messageHandler.saveMessages();
        messageHandler.showMessages();
        EJB3StandaloneBootstrap.shutdown();
    }
}

(10) This uses a simple SLSB like:

package org.nkl.ejb;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.nkl.model.Message;
import org.nkl.model.MessageHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Stateless
public class MessageHandlerBean implements MessageHandler {

    @PersistenceContext
    private EntityManager em;

    Logger logger = LoggerFactory.getLogger(MessageHandlerBean.class);

    public void saveMessages() {
        logger.info("In saveMessages");
        em.persist(new Message("Hello World"));
    }

    @SuppressWarnings("unchecked")
    public void showMessages() {
        logger.info("In showMessages");
        List messages = em.createQuery(
                "select m from Message m order by m.text asc").getResultList();
        for (Object o : messages) {
            logger.info(((Message) o).getText());
        }
    }
}

(11) And a Message entity class like:

package org.nkl.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "MESSAGES")
public class Message {
    @Id
    @GeneratedValue
    @Column(name = "MESSAGE_ID")
    private Long id;
    @Column(name = "MESSAGE_TEXT")
    private String text;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "NEXT_MESSAGE")
    private Message nextMessage;

    public Message(String text) {
        this.text = text;
    }

    Message() {}

    // setters/getters not shown.

}

Lets just say there are a number of stumbling blocks along the way in this approach.

Notably to do with the order of dependencies.

Good luck!!