As we’ve seen, JUnit is a very capable tool for defining, running, and reporting on unit tests for basic Java code, such as the data access classes, model classes, and utility classes in your system. Enterprise components, however, present a unique challenge when it comes to unit testing. In order to effectively and accurately test enterprise components , you need to have a way to drive the components (i.e., provide them with the environment settings and inputs that they’ll encounter at runtime) and capture the results so that they can be verified against expected results.
At a high level, the two general approaches to testing enterprise components are:
In this scheme, the enterprise component is instantiated in a standard Java virtual machine, and the inputs and environment settings for the component are simulated using special implementations called mock objects. The advantage of the mock objects model is that it can be applied in any environment and does not require a full enterprise server environment. The disadvantage is that your components are not tested in a true container environment, which could lead to inaccurate or misleading test results in some cases.
In this scheme, the enterprise components run within an actual container environment, and the test environment drives the tests, either from within the server environment or externally through a special client. This approach has the advantage ...