C H A P T E R  9

Testing Enterprise Beans

As you develop enterprise beans, you might find it expedient to test them before doing a full-scale application assembly and deployment to a production application server. Using the Sun ONE Studio IDE, you generate a J2EE application for this purpose, including a web module with JavaServer Pagestrademark (JSPtrademark) test pages and an EJB module for your bean. You then use the IDE's test feature to display the JSP page's resulting HTML page in a web browser. In the HTML page, you can create instances of an enterprise bean and exercise the bean's methods.

The objects that the IDE creates for you are designed for use during the test process. They are not intended for deployment in a production environment.

You can use the IDE's testing feature with any supported database and application server. The following instructions and example use PointBase as the test database, the J2EE RI application server (the RI) as the test server, and Netscape Navigator as the web browser.


Fulfilling the Prerequisites for Testing

Set-up can vary according to bean type. Your testing set-up can vary depending on which type of enterprise bean you want to test. Some preparation pointers follow.

Message-driven beans are not supported by the testing feature in this release of the IDE.

Variations on the set-up for enterprise bean tests are described in If You Want to Test a CMP or BMP Bean.

Preparing to Deploy to the J2EE RI

To use the IDE's testing feature on an enterprise bean, you must be able to deploy your bean's application to the RI, which must be installed locally on your machine. At least one server instance of the RI must be running.

The RI server installation and instantiation should have been done automatically when you installed the IDE. If you need detailed instructions, refer to the RI's online help.

Preparing to Test Beans Against the PointBase Database

You can use the PointBase database, which is included in the IDE, to test any enterprise bean that needs access to a database. (Normally, this means only entity beans.)

Set these properties as follows:

1. In the IDE's Explorer window, right-click the EJB module node and choose Properties.

The module's property sheet appears.

2. In the J2EE RI tab of the Properties window, specify the connection to the database as follows:

Field

Your Input

Data Source JNDI Name

jdbc/Pointbase

Data Source Password

PBPUBLIC

Data Source UserName

PBPUBLIC




Note - In this window, type Pointbase with an initial capital only. After you type the password, press Enter.



Two of the RI's default settings make the server create a table for the bean being tested, and drop the table when testing is done. If your bean must have live data from a particular database table, make the following change:

3. Click the SQL Deployment Settings field and then the ellipsis (...) button.

The SQL Deployment Settings property editor appears.

4. Select the bean you want to test, and clear the following two checkboxes so that they are blank:

5. Click OK and dismiss the EJB module's property sheet.

6. Save your work with File right arrow Save All.

Starting the Database Server and Web Browser

To start the PointBase database server, choose Tools right arrow PointBase Network Server right arrow Start Server from the main menu.

Launch your web browser as you usually do.

You might want to minimize both of those windows, but don't close them down until you're finished with your testing activities.


Generating Test Objects

Now you're ready to use the IDE's wizard to provide the EJB module, the web module, and the application with which you will test your enterprise bean.

Follow these steps to generate test objects for your enterprise bean:

1. In the Explorer window, select the bean's logical node, right-click, and choose Create New EJB Test Application.

A wizard appears, showing default values for all the components needed to test your application.



Note - If you right-click one bean and see that the Create New EJB Test Application menu item is disabled, the bean probably has no remote interfaces. Follow the directions in Adding Remote Interfaces to a Bean and try again.



Notice that the Package field displays the current package name where your bean resides. You can move the EJB module and test application objects that the wizard is about to create by typing other package names into those fields.

In the next fields, you can also specify other package and module names if necessary. However, if you plan to substitute an existing EJB module for the one that this feature will generate, don't specify the existing module here. Instead, you'll use the application's property sheets later in the process.

Notice the selections in the Application Server combo box. You can use the RI as your testing server, or you can use another server that you have specified as your default server.

2. Mark the Auto Deploy checkbox or leave it blank.

If you mark the checkbox, the IDE automatically deploys your bean's testing module to the server as soon as you've finished creating it in the wizard. You might want to use this approach if the bean you're testing is stand-alone and needs no other beans to do its work.

If you leave the checkbox blank, you will handle the deployment yourself in a later step. Use this approach if your bean needs to be tested in conjunction with other beans.

3. Click OK to generate the EJB module, the web module, and the application, and (if applicable) to deploy the application automatically.

A progress monitor tells you how the module generation and deployment are going. When deployment is complete, a message appears in the IDE's log window.

Assuming that you have let the wizard place what it produced in the bean's package, the generated objects for a bean named Account in a package named Accounts_North would look like the example in FIGURE 9-1.

 FIGURE 9-1 Example of Test Objects Generated for Enterprise Beans

Screenshot showing an example enterprise bean with its generated testing objects in the Explorer.

Notice the generated objects that have been added to the bean's package:

Unless you specified otherwise in the wizard, the IDE has placed the actual web module (containing JSP pages and helper Java classes) in a new filesystem of its own.

Notice also that the wizard created a new filesystem to contain the web module and other objects for use in testing. When you expand the filesystem node, you see the web module as the first subnode. This web module, shown next, contains the JSP pages and helper Java classes.

Screenshot showing the web module node expanded in the Explorer.[ D ] 

You have now generated the basic test application. If your bean references any other beans, add them to the EJB module as follows:

4. In the Explorer window, select the generated EJB module node, right-click, and choose Add EJB.

5. In the tree view, navigate to the referenced enterprise bean, select it, and click OK.

The referenced bean is added to the EJB module, and a reference to the bean is added to the test application.

Repeat Step 4 and Step 5 for each referenced bean.

Now, unless you marked the Auto Deploy checkbox in Step 2, you're ready to deploy your test application to a server. Or, if you prefer, you can deploy and execute the application in one step. The following sections describe each approach.


Deploying the Test Application to a Server

To deploy your test application to the RI, do the following:

1. In the Explorer window, select the J2EE application node, right-click, and choose Deploy.

A progress monitor and output window tell you how the deployment is going. You see a message, for example, indicating that the test application has been deployed on the RI (which is referred to as localhost).

2. Verify that your test application has been deployed by opening the J2EE command window and checking for the statement "Application name_TestApp deployed."



Tip - If your deployment failed, check to see that you set the IDE to run the J2EE RI properly. Especially, notice whether the RI_Home property is set to your J2EE_HOME value.



When the deployment is successful, you can follow the steps under Executing the Test Application.

Deploying and Executing the Test Application in One Step

To deploy your test application to a server and start it executing at the same time, do the following:

single-step bulletIn the Explorer window, select the J2EE application node, right-click, and choose Execute.

A progress monitor and output window tell you how the deployment and execution are going. You see messages, for example, indicating that the application server has been contacted, the enterprise bean or beans have been deployed on the server (notice that the RI is referred to as localhost), the wrapper and RMI-IIOP code have been compiled, the JAR or JARs for the server and client have been made, the web server has been contacted and asked to run the test application, and all the generated code has been saved.

When this double step is complete, a web browser appears and opens to the test client, a JSP page that contains the GUI for testing your enterprise beans. An example is shown in FIGURE 9-2.

Continue at Using the Test Client to Test Your Beans.


Executing the Test Application

If you didn't choose Execute to deploy and execute your test application in one step, you can do the following to test your enterprise bean or beans:

single-step bulletOpen a web browser and type in the appropriate URL.

This URL has the following format if you are using the J2EE RI:

http://localhost:port/application_name/

port is the port number you indicated when you installed the RI.

application_name is the name of the application.

The test application's client, a JSP page, appears in the browser.


Using the Test Client to Test Your Beans

You follow the instructions on the test client's JSP page to create instances of your enterprise bean and call its business methods. The following section describes how you might test a very simple session bean called dollarToYen, which converts amounts in U.S. dollars to the equivalent values in Japanese yen. (This session bean happens to reside in the Java package Converter.)

Understanding the Test Client Page

FIGURE 9-2 shows an example of a JSP page that the IDE has created for the application client that was generated to test the example session bean.

 FIGURE 9-2 Client JSP Page Generated to Test Simple Session Bean dollarToYen

Screenshot showing the JSP page generated to test a simple session bean.[ D ]

As shown in FIGURE 9-2, the parts of the testing window are as follows:

1. The browser's URL field shows the location of the test client's JSP page. Your client's URL is generated automatically by the IDE's testing feature. If you like, you can use this URL to return to this testing window.

2. The Stored Objects frame shows the stack of objects created during your testing sequence by the IDE or by your own actions, for example, when you have called a method on an interface or on a bean class. Right now, only the home interface is shown.

You can remove objects from the Stored Objects stack by using the Remove Selected or Remove All button.

3. The EJB Navigation frame shows the objects that the IDE has generated so that you can test your bean. If the bean you're testing has references to other beans and your EJB module contains more than one bean, this frame lists the created objects in logical order, that is, in the order that one bean in the module calls another.

In the EJB Navigation frame in FIGURE 9-2, you see Converter.dollarToYenHome, which shows that the IDE has created the session bean's home interface. Later, you will click this home interface to create and initialize a new instance of the session bean.
When you see more than one object listed in this frame, you click an object to change focus to the bean component you want to test. When you click an object, notice how the other frames change.

4. The Results of the Last Method Invocation frame shows, for example, the last method you called and its parameters. Right now, since we haven't yet begun testing the session bean, nothing appears in this frame.

5. The bottom frame shows the methods that are now available for you to test. This frame changes as you place focus on different components of the bean (listed in the EJB Navigation frame).

Now let's test the dollarToYen bean's home interface and business method.

Testing the Sample Bean's Home Interface

To verify that Converter.dollarToYenHome correctly creates an instance of the session bean, do as follows:

1. In the EJB Navigation frame, click the home interface name.

2. In the bottom frame, click the Invoke button under the home interface name.

In this case, you click the Invoke button under Converter.dollarToYenHome.

Notice the following changes in the page's frames:

Now let's test the dollarToYen bean's business method.

Testing the Sample Bean's Business Method

To verify that the instance of Converter.dollarToYen correctly converts U.S. dollar amounts to Japanese yen, call the bean's business method as follows:

1. In the EJB Navigation frame, click the bean name (under the home interface name).

Notice that the Results frame is cleared and that the bottom frame's list of invokable methods now starts with the bean's business method.

2. In the bottom frame, under the business method, type a parameter into the input field and then click the Invoke button.

As shown next, we use the parameter 100 (and the IDE adds the .0).

Screenshot showing the Invoke Methods on Bean Name frame with a testing parameter entered. 

After clicking Invoke, notice the following changes in the page's frames:

Screenshot showing the Results of the Last Method Invocation frame with testing results. 

You can call any other bean methods that are shown in the bottom frame.

Creating New Testing Classes

When you create an object by invoking the bean's other methods, that object can be used to test the bean further. For example, the objects created so far by testing Converter.dollarToYen also appear in a combo box, as shown in the following example. You can select one of the objects and create a new class to use for testing.

Screenshot showing the Invoke Methods on Bean Name frame with possible new classes offered for selection. 


Making Changes After Deployment

You can make changes in an enterprise bean and retest it without generating another test application. However, you must redeploy the test application before testing a bean any of whose components you have modified. Do this by closing the testing window, going to the Explorer window, right-clicking the test application module, and choosing Execute. Don't try to redeploy a bean during the same testing session.



caution icon

Caution - The IDE generates an EJB module, a web module, and a J2EE application that are designed only to be used to test your enterprise bean. These generated objects are not meant to be modified. If you modify any of them, you might not be able to deploy the J2EE application again.




Preparing to Test: Some Variations

As mentioned in Fulfilling the Prerequisites for Testing, if you're not testing something like a simple session bean that already has its remote interfaces, you need to consider the preparatory steps described next.

If You Want to Test a CMP or BMP Bean

You can test an entity bean in one of the following ways:

Either way, when you're testing an entity bean, you must use an EJB module in which the bean's properties are declared. Since the EJB module generated by the testing feature is designed only for test use, you will also have to create a "real" EJB module for your production entity beans. You can do this before or after testing.

If You Want to Test a Bean With EJB References

If you want to test an enterprise bean that is designed to interact with another bean, (for example, if you're testing a session bean that manages work done by an entity bean), you must make sure the referenced bean is included in the EJB module.

You can take either of the following approaches:

Before you generate the test application, make sure that all necessary EJB references are specified in the beans' property sheets, and that any necessary overrides are specified in the EJB module's property sheet. Otherwise, the test might fail or give mixed results.

See for details.

Adding Remote Interfaces to a Bean

If the bean you're testing has only local interfaces, you can prepare it for testing by copying the local interfaces to create remote ones. Briefly, you use existing interfaces as follows:



caution icon

Caution - Don't try to use this process to add EJB 2.0 features (such as local interfaces or references) to an EJB 1.1 CMP entity bean that has not been fully updated to conform to the current version of the Enterprise JavaBeans Specification. The resulting bean will be invalid and irreparable. See Avoiding the Use of New Features in an Old Bean for details.



Add remote interfaces one at a time. (These instructions start with the local interface and the corresponding remote interface. Later you will repeat the process with the local home interface and the home interface.)

Do as follows:

1. In the Explorer window, right-click the interface node labeled LocalBean_name and choose Copy from the menu.

2. Select the bean's Java package, right-click, and choose Paste right arrow Copy.

A copy of the interface appears in the folder, labeled LocalBean_name_1.

3. Select the copy, right-click, and choose Rename. In the Rename dialog box, name the copied interface according to the J2EE convention: Bean_name.

4. In the Explorer, right-click the bean's logical node and choose Customize.

The Customizer dialog box appears.

Notice that the Customizer deals with many of the properties also available in the property sheet that appears when you right-click the bean's logical node and choose Properties. You can change many of the bean's properties in either dialog box. However, the Customizer does deals only with properties on the bean itself, not with properties relating to application servers. Also, if you want to add remote interfaces to a bean, you can do so only in the Customizer.

5. In the Customizer dialog box, find the empty Remote Interface field and click the Browse button.

The Select a Class file chooser appears, as shown in FIGURE 9-3.

 FIGURE 9-3 Customizer for Adding an Interface Class to a Bean

Screenshot showing two dialog boxes: the Customizer and a file chooser.[ D ]

6. Navigate to the interface class you created by copying and pasting. Select the interface node and click OK.

The class appears in the Remote Interface field.

(You will see later that the Remote Interface field in the bean's property sheet has automatically been updated.)

7. Dismiss the Customizer dialog box.

8. In the Explorer, expand the logical bean node. Expand the Classes node, select the new remote interface (labeled Remote Interface Class), and open it in the Source Editor.

Edit the remote interface class to extend javax.ejb.EJBObject.

9. For each method in the remote class, add the exception java.rmi.RemoteException.

10. For each ejbCreate method, change the return type to the new remote interface.

11. For each finder method that finds a single object, change the return type to the new remote interface.

12. Compile your enterprise bean and resolve any errors.

13. Use the same process (Step 1 through Step 12) to create a home interface from a copy of the local home interface.

However, in Step 8, edit the home interface class to extend javax.ejb.EJBHome.



Note - Be sure to remove from the two new interfaces any methods that cannot be used as remote methods.