The EJB Session Bean Unit Testing with JUnit

The EJB embedded container is introduced by the Glassfish Application Server version 3 and it is a core part for unit testing. It’s light weight and strong enough so that the EJB unit testing can be executed without the starting of Glassfish Application Server. For sure, you may imagine it can be applied for other purpose as same as the embedded database.

Via NetBean, you can create the JUnit test case and test suit simply by right clicking at the node named “Test Packages” of your EJB project and choose the one you want. The following is the simply step for initiating the EJB embedded container: –

Creating the JUnit test case or test suit

1. Declare and initiate the EJB embedded container.

private static EJBContainer ejbContainer = EJBContainer.createEJBContainer();

2. Declare and initiate the JNDI context for looking up the EJB.

private static Context context = ejbContainer.getContext();

3. Simply use the context object for looking up your EJB and another JavaEE resources via JNDI, for example, The connection pool, the EJB and so on.

4. The rest is creating your test respectively to you business, unit or others test case by following the JUnit standard.


1. The EJBContainer is javax.ejb.embeddable.EJBContainer

2. The Context is javax.naming.Context

Executing the test case with the EJB embedded container

The prerequisite

1. The EJB application is not required to be deployed to the Glassfish Application Server

2. The Glassfish Application Server must not be started since the ports will be clashed since the embedded will share the same ports.

The execution

Just simply right click at your test case or test suit and choose “Run File”.


About Charlee Chitsuk

I've been working as a software developer since 1998. At the moment I focus on system integration including with SOA and Enterprise Security Solution.

Posted on June 24, 2010, in EJB, Unit Testing and tagged , . Bookmark the permalink. 1 Comment.

  1. developers stuff

    can you provide me any example code for this. Thank you

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: