$Revision: 1.10 $

This tutorial will show you how to use JUnitEE to unit test an Enterprise JavaBean. All sources are included in the example directory of the JUnitEE distribution and are also external windowbrowsable online.

Implement the EJB

As this is a tutorial on testing and not on implementing EJBs I will not go into details on how to write Enterprise JavaBeans. I assume that you already have some knowledge about this kind of stuff, otherwise you would not be interested in testing EJBs.

The EJB used for this tutorial is a simple session bean which adds two numbers. Have a look at the sources in the package org.junitee.ejb.einstein to see what is going on in the bean. The method addTwoNumbers is a little bit buggy to show the effect of a test failure, the method emc2 just throws an exception to cause a test error.

Write the test case

The test case is a standard JUnit TestCase. For the fixture, you can use the default JNDI InitialContext to get a reference to the EJB like this:

protected void setUp() throws Exception {
  Context jndiContext = new InitialContext();

  Object einRef = jndiContext.lookup("java:comp/env/ejb/EinsteinEJB");
  EinsteinHome home = 
      (EinsteinHome)PortableRemoteObject.narrow(einRef, EinsteinHome.class);

  this.ein = home.create();

And the test methods themselves look like this:

public void testSimpleAddition() throws RemoteException {
	String result = this.ein.addTwoNumbers("7", "10");

The full source of the test case is included in the example - see package org.junitee.ejb.einstein.test.

Create the frontend web form

The JUnitEEServlet will execute test cases specified by the "suite" form parameter, which can appear multiple times. It's nice to have a simple form to start the tests:

	You may type in the name of a test suite:
	<form action="TestServlet" method="get" name="youTypeItForm">
		<input type="text" name="suite" size=60 />
		<input type="submit" value="Run" />
	You may pick one or more of the following test suites:
	<form action="TestServlet" method="get" name="youPickItForm">
		<select name="suite" size="2" multiple>
			<option value="org.infohazard.test.EinsteinTest">
			<option value="some.other.Test">
		<input type="submit" value="Run" />

Create the web.xml deployment descriptor

The web application must have a deployment descriptor which provides the ejb-ref mapping so that the "java:comp/env/ejb/EinsteinEJB" JNDI lookup will work. And the JUnitEEServlet and it's URL mapping is also required in the web.xml. Here is an example of how it should look like:

<?xml version="1.0"?>
        "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" 

  <display-name> Einstein Unit Tester Web Application </display-name>

    <description>JUnitEE test framework</description>



IMPORTANT NOTE: Orion Application Server has a bug in it's url pattern matching algorithm that causes some problems for JUnitEE. If you are using Orion please change the servlet mapping in the deployment descriptor to


Package the test WAR

Now you have everything ready to package the test web application. You have to put

  • junit.jar and junitee.jar to WEB-INF/lib
  • your test classes to WEB-INF/classes or a jar containing the test classes to WEB-INF/lib
  • the test frontend form to index.html
  • and the web.xml to WEB-INF

Finally, jar everything into the test.war and and create an EAR which contains test.war and the ejb-jar containing the Einstein bean.

Run the tests

After deploying the EAR point your browser to the index.html included in the test.war and select the EinsteinTest to be executed. After a few seconds your browser will display the test report showing successful and not so successful tests.

Make your life easier

Now you have seen how to create a .war file containing the JUnitEE servlet and your test classes. It requires some steps to create the war, but fortunately there is a way to automate these steps: use the JUnitEEWarTask created for use with Ant. Have a look at the Ant HowTo to learn more about this task.


Some things you might want to keep in mind when unit testing EJBs:

  • Unless you (and everyone else on the project) are extremely careful writing your unit tests, they are very likely going to seriously mess up your database. You should probably never run tests against a production system, and for this (and other security reasons) you should not enable the test web application on the production machine.
  • The testing of getters and setters is probably a waste of time.

Stuff you might find interesting and/or useful:

This project is hosted by  SourceForge Logo