Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing an EJB

I'm looking for a way to apply TDD to Session Beans.

can anyone provide advices and links on how to unit test them ?

how to use JUnit to do so ?

P.S : I'm new to Test Driven Development and Session Beans.

I'm using EJB v2.

like image 425
Attilah Avatar asked Sep 29 '09 12:09

Attilah


2 Answers

I'm assuming you are talking about EJB2.x Session Beans. For these kind of animals, what I like to do is:

  • Use the Session Bean as a wrapper that just delegates logic to a POJO that you can test easily outside a container. Outside container testing is better, faster, easier, etc but won't cover things such as deployment descriptor validation - and/or -
  • Use something like Cactus for in-container testing (check the Howto EJB documentation) - and/or -
  • Build and deploy your EJB module with Cargo for integration testing.
like image 169
Pascal Thivent Avatar answered Oct 06 '22 11:10

Pascal Thivent


You don't say which version of EJB you're using. If it's EJB v3, check out Ejb3Unit. From the website:

Ejb3Unit is a JUnit extention and can execute automated standalone junit tests for all EJB 3.0 conform Java EE projects. The out of container test approach leads to short build-test-cycles, because no container deployment is necessary anymore.

However I would advocate separating out functionality from the EJB-specifics. This will allow you to test complex functionality outside the container and without using frameworks such as the above. The majority of your tests would be testing POJOs (plain old Java objects) and relatively few would focus on your persistence framework testing.

EDIT: So if you're using EJB v2 then obviously ignore the first point. The second point remains valid, however.

like image 44
Brian Agnew Avatar answered Oct 06 '22 12:10

Brian Agnew