lupestro (7) [Avatar] Offline
#1
Modern development practices demand unit testing of most code. When you do the new edition of this book, it would be useful if you could devote part of a chapter in part 4 to strategies for automated and _isolated_ unit testing of business logic code that uses EJB3 and JPA.

Having thought about this, I think there's really only one key mechanism needed - a way for tests to access the same DI mechanism that the EJB container uses, so the test can supply mock implementations of the resources, the Entity Manager, etc. These can populate data or throw exceptions so all of the code paths can be tested independently of the framework. Are all of the types used as DI targets by EJB3 interfaces or are there concrete classes, too?

Would you have any general advice in this regard? Does EJB 3.1 provide any mechanisms that can be leveraged for this, with maybe a utility class or two in the test suite? Or do I need to use an external toolset that simulates EJB3 DI?

Lupestro
reza_rahman (456) [Avatar] Offline
#2
Re: Unit testing of EJB3 or EJB3.1 code
Lupestro,

We do indeed plan an entire chapter on testing for the second edition.

The key tool for unit testing EJB 3.x is an embedded container, which is standardized in EJB 3.1. The best tool in EJB 3.0/Java EE 5 is Apache OpenEJB, which supports things like alternative deployment descriptors, etc. You can use things like embedded databases (e.g. HSQL) and DBUnit with OpenEJB. The OpenEJB community is also pretty active about implementing any feature requests. You might also want to check out EJB3Unit (not sure about its level of maturity).

Cheers,
Reza
lupestro (7) [Avatar] Offline
#3
Re: Unit testing of EJB3 or EJB3.1 code
Thanks Reza,

> The key tool for unit testing EJB 3.x is an embedded container, which is standardized in EJB 3.1.

Did they pick OpenEJB as the RI? Or are they using something else? Does this mean that I should have everything I need in Glassfish V3 and Netbeans 6.8? If OpenEJB was used as the RI, can I use the documentation on the OpenEJB site for it or did they change class and property names into the javax space.

> Apache OpenEJB ... supports things like alternative deployment descriptors, etc.
> You can use things like embedded databases (e.g. HSQL) and DBUnit with OpenEJB.
> You might also want to check out EJB3Unit (not sure about its level of maturity).

OpenEJB looks like it will be really good for unit-focused integration testing, making sure the unit integrates with the container properly. EJB3Unit looks like it is ideal for the kind of code path coverage I'll want to do as a part of every time the code builds, since it will let me mock away the container completely and run like lightning.

So, two good tools with complementary strengths. More to learn, always more to learn. smilie

The original EJB3Unit project team seems to have gotten busy with other things but there is some new blood involved at least in bug fixes to the version 1 stream. They had plans for a 2.0 stream that would support EJB3.1 and JMock2 and there's a snapshot for it out there. It doesn't appear to have been under active development for about a year but looks can be deceiving. I'll have to see how far from ready the 2.0 code stream is and grab the source. If I find myself using it enough, maybe I can help.

Thanks again,
Lupestro
reza_rahman (456) [Avatar] Offline
#4
Re: Unit testing of EJB3 or EJB3.1 code
Lupestro,

OpenEJB is not the RI solution, embedded GlassFish is. OpenEJB was the model that was standardized. However, I've used OpenEJB with both JBoss and GlassFish since it allows for configuration of JNDI names (now standardized in EJB 3.1 so a moot point). If you are a GlassFish guy, feel free to check out Adam Bien's blog. He has good material on using OpenEJB with GlassFish I think...

I do think the EJB3Unit guys could use a hand...

Cheers,
Reza