Testing a Session Bean

You can create your own client that accesses the enterprise beans, but the IDE also includes a mechanism for testing enterprise beans without having to manually create a client test application. This feature creates a test application and packages it along with the enterprise bean. You can use this test application to create an instance of the enterprise bean and interact with it.

Testing a session bean follows these steps:

  1. Creating a Test Client for the Session Bean
  2. Deploying the Test Client
  3. Using the Test Client to Test the Session Bean

Creating a Test Client for the Session Bean

To create the test client:

  1. Create EJB module, web module, and J2EE test application.
  2. Right-click on the session bean logical node and choose Create New EJB Test Application. The EJB Test Application wizard appears. Accept all default values and click the OK button.

    A progress monitor appears briefly and then goes away when the process is complete. Another window appears informing you that the web module that was created is also visible in the Project tab. It should go away automatically also. If not, click OK to close the window.

  3. View the resulting test objects in the Explorer.
  4. The IDE has created an EJB module named session_bean_name_EJBModule, a web module named session_bean_name_WebModule, and a J2EE application named session_bean_name_TestApp in the ejb subdirectory. The EJB module contains the session bean, which is to be tested. The web module contains a number of JSP pages and Java class files that support the test client. The J2EE application includes references to the EJB module and to the web module. In addition, the IDE mounts the web module.

NOTE - The EJB module, web module, and J2EE application generated by the IDE are not intended to be modified. If you modify them you might not be able to redeploy the J2EE application.

Deploying the Test Client

Deployment is the process of compiling the source files that make up a J2EE application and installing the compiled files into directories managed by a J2EE application server.

NOTE - Make sure that the Sun ONE Application Server instance is set as the default server and is running.

To deploy the test application:

  1. Right-click the session_bean_name_TestApp J2EE application node and choose Deploy.
  2. As the deployment progresses, you see a progress bar window showing you what phase the deployment is in. See the new tabs in the IDE's Output window that display messages about the deployment.

  3. Check for a successful deployment.
  4. Click the server1(localhost:port):server.log tab in the Output window.

    The server.log view displays any error conditions that prevent deployment.

    Click the Editing tab and then click the Explorer's Runtime tab. Expand the Server Registry -> Installed Servers -> Sun ONE Application Server 7 -> default_server -> server_instance -> Deployed Applications nodes.

    You should see the session_bean_name_TestApp node under the Deployed Applications node. If the session_bean_name_TestApp node is not listed, right-click on the Deployed Applications node and choose Refresh List.

Using the Test Client to Test the Session Bean

Point your web browser to the test page to start the application. On the test client's web page that is displayed, use the create method of the session bean's home interface to create an instance of the bean. Then test a business method on that instance.

To test the session bean:

  1. Start a web browser and point it to the following URL:
  2. http://localhost:80/session_bean_name_TestApp

    Note - Port 80 is the default port Sun ONE Application Server runs on. If you need to verify that this is the correct port, pointing your browser to http://localhost:80/ should display the welcome page of the HTTP server.

    Your browser displays the test client.

    From this page, you can invoke the methods of the session_bean_nameHome remote interface.

  3. Click the Invoke button next to the create method to create an instance of the session bean.
  4. The create method is under the heading "Invoke Methods on package.session_bean_nameHome."

    The web browser will now display the new session bean instance as shown below:

    The instance of the session bean created by invoking the create method on the session_bean_nameHome interface is added in the instances list.

  5. Click the package.session_bean_name instance link in the instances list (upper left).
  6. This action selects the instance as the EJB module to test. The web browser now displays the methods that can be invoked, including the bean's business method.

  7. Click the Invoke button next to the business method.

    The business method is called, and its return value is listed in the Results area:

  8. In the IDE, click the server1: access tab in the output window.
  9. The access view displays the browser accesses recorded in the access log like this:



Legal Notices