I'm working on a Java EE 6 application. When I started out, I was writing tests for my EJB classes by manually instantiating the EJB, then manually adding the members that normally get provided by dependency injection. As the application gets more complicated, I find that this approach just doesn't cut it. So I'd like to be able to start my own EJB container in the test framework, so it can manage my beans. What's the best way to approach this? I've heard of javax.ejb.embeddable.EJBContainer
, are there other options?
(I'm using Glassfish 3, and building with Maven, if that makes any difference.)
The main objective of unit testing is to isolate written code to test and determine if it works as intended. Unit testing is an important step in the development process, because if done correctly, it can help detect early flaws in code which may be more difficult to find in later testing stages.
UNIT TEST THE EJB System can generate a web service that is used to test the EJB. Right click on CustomerManagerControl. jcx and select Generate Test JWS File (Stateless). This will create a new web service called CustomerManagerControlTest.
If you expand the Test Libraries node in the Projects window, you can see that the IDE automatically added GlassFish Server (embeddable container) and JUnit 4. x as test libraries.
What exactly are you testing? Logic? Configuration? Do you NEED to test the EJB classes directly? Would it suffice for your tests to behave as an EJB client against a running container? (Remember there's no rule that says automated unit tests can't require a running system-under-test.)
If it's business logic you need to test, move that code into POJOs and test normally; you wouldn't need to then test the POJOs running in a container, as the container shouldn't affect the business logic.
In a related situation, I have never directly JUnit-tested a servlet class or a Struts controller class. I definitely test the POJOs those depend on, and I test the end application (running in a servlet container, tested with HtmlUnit), assuming that if the end app works, then the plumbing works too.
I've heard of
javax.ejb.embeddable.EJBContainer
, are there other options?
The EJBContainer
API is an option. The other one would be to use Arquillian (and SchrinkWrap).
Some more links:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With