Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Unit Testing EJB3

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

Christian.

 
Rafael De la Guetto
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic