2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.


This document explains how to unit test EJBs version 3.0 with Cactus. It is a good idea to have a look at the sample applications that come with Cactus's distribution in samples/ejb3

This tutorial shows how to write test cases for unit testing EJB methods. The EJB used in this example is a simple Stateless Bean.

This tutorial assumes the reader has already understood how to use Cactus to test Servlet. To test an EJB, you need to prepare a war to contain the cactus setting and test cases, and then prepare a jar of your ejb. You need to match the JNDI name in the ejb jar and the war.


Step 1: Prepare the Sample EJB

The EJB used in this tutorial is a simple Stateless Bean with a method for converting Yen to Dollar.

EJB Bean

package org.apache.cactus.sample.ejb3;

import javax.ejb.Stateless;

import org.apache.cactus.sample.ejb3.IConvertLocal;

* Sample EJB3 bean.
* @version $Id: ConverterBean.java 238816 2008-06-31 16:36:46Z ptahchiev $
public class ConverterBean implements IConvertLocal {
     /* (non-Javadoc)
      * @see org.apache.cactus.sample.ejb3.IConvertLocal#convert(double)
      public double convert(double theYenAmount) {
         return theYenAmount / 100.0;

EJB Local Interface

package org.apache.cactus.sample.ejb3;

import java.io.Serializable;

import javax.ejb.Local;

 * Sample EJB local interface
 * @version $Id: IConverterLocal.java 238816 2008-06-31 16:36:46Z ptahchiev $
public interface IConvertLocal extends Serializable
      * A method declaration to convert yen to dollars.
      * @param theYenAmount
      * @return
      public double convert(double theYenAmount);

Prepare deployment descriptors


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE application
   PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN'
   <description>Cactus EJB3 Sample</description>


Cactus Web Application

Step 1: Prepare test code

package org.apache.cactus.sample.ejb3;

import java.util.Properties;

import javax.naming.InitialContext;

import junit.framework.TestCase;

import org.apache.cactus.ServletTestCase;

 * Sample Cactus test for a session bean.
 * @version $Id: TestConverterEJB.java 238816 2004-02-29 16:36:46Z vmassol $
public class TestConverterEJB extends ServletTestCase
       * Class under test
      private IConvertLocal converter;
       * @see TestCase#setUp()
      public void setUp() throws Exception
         Properties properties = new Properties();
         properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
         properties.put("java.naming.factory.url.pkgs", "org.jboss.naming rg.jnp.interfaces");
         properties.put("java.naming.provider.url", "jnp://localhost:1099");
         InitialContext ctx = new InitialContext(properties);
         converter = (IConvertLocal) ctx.lookup("samples-ejb-1.8.1-SNAPSHOT-cactified/"+ConverterBean.class.getSimpleName()+"/local");
       * Verify yen to dollars conversion works.
       * @throws Exception on error
      public void testConvert() throws Exception
         double dollar = this.converter.convert(100.0);
         assertEquals("dollar", 1.0, dollar, 0.01);

Step 2: Prepare Web Application Deployment Descriptor (web.xml)

<?xml version="1.0" encoding="UTF-8"?>
	<!DOCTYPE web-app
						PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"



Step 3: Package Web Application

Package the web application into a war file.


You need to package the war file with all the additional jar files in an ear file and then deploy the ear file based on the deployment procedure of your servlet container and your ejb container.