Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB testing strategies?

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.)

like image 343
Mike Baranczak Avatar asked Sep 15 '10 20:09

Mike Baranczak


People also ask

What is the purpose of unit testing?

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.

How do I test EJB in Weblogic?

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.

Which server is used by embedded container test?

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.


Video Answer


2 Answers

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.

like image 50
Rodney Gitzel Avatar answered Nov 01 '22 09:11

Rodney Gitzel


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:

  • Real Java EE Testing with Arquillian and ShrinkWrap (slides + audio)
  • Test drive with Arquillian and CDI (Part 2)
  • Arquillian in Action; New Features Coming Soon
like image 25
Pascal Thivent Avatar answered Nov 01 '22 09:11

Pascal Thivent