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).
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.
Rafael De la Guetto
Joined: Jan 10, 2008
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...