Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Unit Testing EJB3

Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Rafael De la Guetto
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
What's gotten into you? Could it be this tiny ad?
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic