Enterprise JavaBeans (EJB) has become increasingly less popular among J2EE developers in the last couple of years. Many factors contribute to this poor reputation, but high on the list is the fact that EJB components are inherently difficult to test.
The EJB 3.0 Expert Group recognizes that testing EJB applications proves difficult, and the upcoming specification is expected to address the issue. However, at the time of this writing, EJB 3.0 is still in early draft review (see Resources). The specification's finalization and widespread implementation by vendors is still many months away.
In a typical current EJB application, business logic hides behind session-bean facades. For performance reasons, a large portion of the code running on the EJB container is exposed only via EJB local interfaces, which are inaccessible to a suite of JUnit tests running on a separate JVM. In the case of container-managed persistence (CMP) entity beans, much of this code also relies on container-provided services, making it virtually impossible to run outside the EJB container. For example, the EJB CMP bean classes that the application developer writes are themselves abstract classes, the concrete implementations of which are provided by the container. In short, creating a suite of comprehensive unit tests for an EJB-based application creates a real headache for any proponent of test-driven development (TDD).
Two approaches tackle this problem. First is the use of mock objects, which involves a dummy implementation of the EJB API, that can run outside the EJB container. An example is MockEJB. The second approach involves the use of an in-container testing framework such as Cactus, from the Apache Jakarta Project. Here, the idea is to deploy the test code onto the container and run the tests in the application's real execution environment.
In this article, I present a framework that follows the in-container test approach, but has the advantage of being simple to understand and use. It involves the following:
Before we discuss how the framework works, let's first consider our requirements for an EJB testing mechanism:
lib directory to enable the framework.
Let's now explore how our framework is implemented.
Archived Discussions (Read only)