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 test pattern, the EJB business logic test pattern, and the EJB session facade test pattern.

With the Create Test for EJB wizard, 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.

Note: You cannot test an EJB through its local interface and through its remote interface within the same component test or test suite. You must create one component test to test through the local interface and a second component test to test through the remote interface. You can store the two component tests in the same test project.

EJB lifecycle test pattern

Use the EJB lifecycle test pattern to create tests for verifying the lifecycle methods for an EJB (creating new beans, removing beans, and finding beans). For each lifecycle method published in the local home or home interface, the bean class for the EJB contains a corresponding callback method that defines 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. For stateful session beans and entity beans, this test pattern also checks whether the state of the bean is correct. One use of the lifecycle test pattern is 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 bean class of the EJB. 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 state of the bean, 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 test pattern

Use the EJB business logic test pattern to verify 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. Also, use the test data table to do data-driven testing on a wide range of input data.

EJB session facade test pattern

Use the EJB session facade test 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. The session facade test pattern is often used to test a session bean that calls a local entity bean. The session facade test pattern generates tests for the session bean and for those methods in a subsystem (usually an entity bean) that are called by the session 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("",...);


     CardAccountHome cardAccountHome=getHome("",...);     
     CardAccount cardAccount=cardAccountHome.findByPrimaryKey(accountId2);

     bankAccount.withDraw(amount);

     cardAccount.deposit(amount);

}

The transferFunds method calls the following methods:

In this case, the TransferFunds EJB is the facade, and the subsystem-under-test consists of the BankAccount and CardAccount EJBs. You can use the Create Test wizard to generate a test of the methods in the facade and those methods in the subsystem that are called by the facade: the two findByPrimaryKey methods, plus 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
Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.