As of now, Ejb3Unit seems to be the best way to unit test EJB3. Actually there are not many alternatives. You can either do plain in-container testing or use embedded EJB3 container from JBoss. Where the first is just too bulky and the second is very buggy (at least it gave me so many problems that I was forced to quit).

Unfortunetly there is not much documentation available on Ejb3Unit. There is this doc file found on the project’s website. But it’s got some gotchas and here is my time saving list.

Entity Beans with Relations

I don’t think testing persistent objects (entity beans) is interesting at all but I think it would be useful to write couple tests on them just to get some feel of the framework.

Gotcha #1

In order for your OneToMany, ManyToOne and ManyToMany relations to work you need to have the annotations on both sides!

Session Beans

Not really a gotcha but at least a small time saver.

Gotcha #2

In the array of USED_ENTITY_BEANS you need to specify all entity beans that are referenced somewhere in code of the classes used in your test (both tested session beans and entity beans used for loading data into db).


Testing with in-memory database is pretty straightforward but using a custom database might cause some problems.

Gotcha #3

Add ejb3unit.inMemoryTest=false to your file!
If its missing, no matter what’s written in the rest of the file, the in-memory db will be used.

P.S. I thought this list would be longer but it’s either my memory or things aren’t that bad :)