aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Testing entity beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Testing entity beans" Watch "Testing entity beans" New topic
Author

Testing entity beans

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29274
    
140

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
wow!, without the server running... please post your solutions when you find it.
cactus might help, but without the server running.. hmm...
good luck


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Stefan Bell
Ranch Hand

Joined: Aug 26, 2003
Posts: 82
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

Joined: Jul 18, 2001
Posts: 2712
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29274
    
140

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.
 
subject: Testing entity beans
 
Similar Threads
Unit Tests and Session Beans
How to write such an EJB-QL
Which one will created first
Violation of Separation of Concerns
give me example code