Test patterns for Enterprise JavaBeans

You can use the EJB test patterns to help you in the process of testing EJBs. These test patterns include the EJB lifecycle testing pattern, the EJB business logic testing pattern, and the EJB session facade testing pattern.

With the automated component testing features, you can test your EJBs remotely through their remote interfaces (remote and home) or locally through their local interfaces (local and local home). When you test locally, the test is deployed on the same application server as the EJB-under-test.

The EJB lifecycle testing pattern overview

Use the EJB lifecycle testing pattern to create tests for testing an EJB's lifecycle methods (creating new beans, removing beans, and finding beans). For each lifecycle method published in the local home or home interface, there is a corresponding callback method in the EJB's bean class defining its implementation. For example, for each create method in the home interface there is a corresponding ejbCreate method in the bean class.

The lifecycle pattern helps you test callback methods by simulating several scenarios that could make these callbacks fail. This test pattern also checks the correctness of a bean's state, in the case of stateful session beans and entity beans. One example of this would be to test the value of the fields in the bean class. To test these values, you supply meaningful data in the test data table.

The lifecycle pattern generates test code automatically for the expected scenarios for each type of bean, thus simplifying your task of testing EJBs.

Lifecycle test pattern for stateless session beans

To test a stateless session bean using the lifecycle test pattern, code is generated to test the ejbCreate() and ejbRemove() callback methods that are implemented in the EJB's bean class. The following callback methods are tested:

The following test scenarios are included:

Lifecycle test pattern for stateful session beans

To test a stateful session bean using the lifecycle test pattern, code is generated to test the bean's state, in addition to the code generated to test ejbCreate() and ejbRemove() The following callback methods are tested:

The following test scenarios are included:

Lifecycle test pattern for entity beans with bean-managed and container-managed persistence

To test entity beans using the lifecycle test pattern, additional code must be generated to test persistence and database lookups. The following callback methods are tested:

The following test scenarios are included:

EJB business logic testing pattern

Use the EJB business logic testing pattern to test business logic. Business logic is usually implemented in session beans, although entity beans can sometimes contain business logic. Be sure to test the business logic itself, that is, the algorithms that process data in parameters and use the test data table to do data-driven testing on a wide range of input data.

The EJB session facade testing pattern

Use the EJB session facade testing pattern to test the implementation of the Session Facade design pattern. This design pattern is used to provide a higher level business logic interface, to hide the complexity of the application from the client, and to improve performance. The Session Facade design pattern, is the EJB version of the well-known J2SE facade pattern. A common use case of the Test Session Facade pattern is to test a session bean that calls a local entity bean. Like the Session Facade pattern, which usually wraps a set of entity beans (the subsystem) in a layer of session beans (the session facade), the session facade testing pattern generate tests for the subsystem behind the facade. A test wizard page lets you select an EJB (usually a session bean) and a set of methods in its business interface.

For example, consider a subsystem that consists of three EJBs:

The TransferFunds EJB contains a transferFunds method, as shown in the following code:

public void transferFunds(String accountId1, String accountId2, int amount){

     BankAccountHome bankAccountHome=getHome("",...);

     BankAccount bankAccount=bankAccountHome.findByPrimaryKey(accountId1); CardAccountHome cardAccountHome=getHome("",...);

     CardAccount cardAccount=cardAccountHome.findByPrimaryKey(accountId2);

     bankAccount.withDraw(amount);

     cardAccount.deposit(amount);

}

Examining the transferFunds method, you will note the following:

In this case, the TransferFunds EJB is the facade, and the subsystem-under-test consists of the BankAccount and CardAccount EJBs. You can use Automated Component Testing to generate a test of the methods in the subsystem, that is, findByPrimaryKey; findByPrimaryKey; withDraw; and deposit.

Related concepts
Test-generation process
Standard Java test patterns

Related tasks
Creating new component test projects
Creating tests for the lifecycle methods of entity beans
Creating tests for the lifecycle methods of session beans
Creating tests for EJB business methods
Creating tests for an EJB session facade

Terms of use | Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.