wood burning stoves 2.0
The moose likes EJB and other Java EE Technologies and the fly likes Unit Testing EJB3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Unit Testing EJB3" Watch "Unit Testing EJB3" New topic

Unit Testing EJB3

Rafael De la Guetto

Joined: Jan 10, 2008
Posts: 9

I would like to know if it is possible to use Junit to test an EJB3 session beans.
My problem arises with the dependency injection of EJBs inside other EJBs. Given that there are no context (like could be in Spring), and that the EJBs are not deployed anywhere, am I supposed to do that instantiation and injection programmatically?
I would be interested in using a fake database (in HSQL) so I would like to automate the injection of all the EJB dependencies with other EJBs, and the automatic injection of a test-purpose EntityManager (which would work with an HSQL database).
This is straight forward if a Spring container was used, i would like to know the way to do it in a raw EJB container (in case there exists one).

Thank you very much!
Christian Gossart
Ranch Hand

Joined: Mar 13, 2008
Posts: 34
Hello Rafael,

You can unit test EJB using the embedded container, which is a new feature in JEE 6. Your EJBs will be deployed, as a real JEE module, and DI will be managed by the container.
You'll find Junit setup information in this CodeRanch post Junit Embedded Glassfish and in this blog Unit testing EJB with Embedded Glassfish. I use this approach for a real world JEE 6 Maven multi-modules project, with continuous integration and testing. I believe EJB 3.0 can be tested as well, because containers are retro compatible. Afaik, some EJB features like JMS/MDB are not fully supported by Glassfish Embedded 3.0.1, but for standard session beans it's the way to go.

There is also Ejb3Unit , a framework I used some years ago, before JEE 6. It's an extension of the Junit framework to ease the test of EJB 3.0.

Hope this helps.


Christian Gossart
Rafael De la Guetto

Joined: Jan 10, 2008
Posts: 9
Hello Christian, thank you for your answer!!
It is exactly what i needed, an embedded container. In the several options that i had, i chose OpenEJB, that is widely used. EJB3Unit seemed to be a great idea, but the project seems to be dead...

Thank you!!
I agree. Here's the link: http://aspose.com/file-tools
subject: Unit Testing EJB3
jQuery in Action, 3rd edition