Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Testing entity beans

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you unit test your entity beans? Is there any way to do it without the server running?
Also, how do you test the session beans that call the entity beans (without the server running) ? The only thing I came up with is to use an interface to get the entity beans and return the real entity bean or a mock bean. But this seems more involved than other tests I've written with junit.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow!, without the server running... please post your solutions when you find it.
cactus might help, but without the server running.. hmm...
good luck
 
Stefan Bell
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andres Gonzalez:
wow!, without the server running... please post your solutions when you find it.
cactus might help, but without the server running.. hmm...
good luck

In my experience with cactus, the server must be running (or you could use junit). I will do a little research and try to get back to you.
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could check out MockEJB for unit testing Session and Message Driven Beans outside of an EJB Container. It currently does not support Entity Beans, though some form of support seems planned for the future. Disclaimer: I have not used this library so I cannot attest to its quality.
I try to keep my business logic cleanly separated from and ejb layer and limit the ejbs to thin facades on top. This then makes unit testing the business logic outside of the EJB Container easy.
For persistance, I usually stay away from Entity Beans (even CMP) and choose instead to wrap the persistance layer with Session Beans. In simpler applications I usually choose plain DAO for persistance or a good O/R mapper like Hibernate for more complex applications. This allows the persistance layer to also be easily unit tested outside of the EJB Container.
In general, I try to stay away from unit testing that requires a running EJB Container when I can.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do use junit for other things. Upon further thought, I could use junit to test the method I'm thinking about adding to the entity bean. The only other custom thing is finders, which I would expect to need a server to test.
Chris, MockEJB looks interesting (although it doesn't do what I'm looking for. I'll have to try it out sometime.) I do have the business logic separated into a separate class that is called from the session bean. I'm not clear on how that makes it easier to test though. I still have that new class that is trying to execute entity beans (or DAOs in your example.)
I agree that it is good to stay away from needing the server for writing tests. I've used MockObjects and it looks like I'll need to do that here as well.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic