Step 4: Test the EJBs

In this step you are going to test the EJBs and examine whether their behavior is as intended. Because EJBs are server-side components, you must first create a client application, such as a web application. Here you will build a test web service instead. This requires generating an EJB control for the EJB, and generating a test web service for the EJB control.

The tasks in this step are:


To Create the Web Service Project

Web service development is done in a separate project in the application.

  1. In the Application tab, right-click the GettingStarted_EJB folder and select New-->Project.
  2. In the New Project dialog, in the upper left-hand pane, select Web Services.
    In the upper right-hand pane, select Web Service Project.
    In the Project Name field, enter MyTestProject.

  3. Click Create.


To Create the EJB Control

When you want to invoke a session bean from a client application, you must first look up the session bean in the EJB container's JNDI registry and obtain a reference to the EJB's home interface, before you can create an EJB instance and invoke the EJB's business methods. You can do this by calling the standard JNDI API methods, or you can use EJB controls that do this for you. In this tutorial you will create an EJB control for the Hello session bean.

  1. Right-click MyTestProject and select New-->Folder. The Create New Folder dialog appears.
  2. Enter helloControl. Click OK.
  3. Right-click helloControl and select New-->Java Control. The New Java Control dialog appears.
  4. Select EJB Control. In the File name field, enter HelloBeanCtrl.

  5. Click Next to go to the next page
  6. Click Browse application EJBs and select Hello (remote jndi).

  7. Click Select.
  8. Click Create.

The EJB control HelloBeanCtrl is created and appears in Design View. Notice that there are no methods defined in the EJB Control. An EJB control is only used to obtain a reference to the EJB. It does not define methods of its own, but simply provides a window to the EJB it exposes. To see exactly which methods are exposed, you need to use the EJB control in a client application such as the test web service that you will generate next.

If you examine the Property Editor, you will see that the EJB control uses the Hello bean's remote JNDI name ejb.HelloRemoteHome to reference its remote interfaces. Remember that this JNDI name was defined when you created the session bean and can be used to look up the EJB within the context of the EJB container.

To learn more about EJB controls, see the help topic EJB Controls. To learn more about Java controls in general, see Getting Started Tutorial: Java Controls

To Generate the Test Web Service

A test web service is a web service that exposes the methods of a Java control. In this particular case the test web service is a client application that calls the methods on the EJB control helloBeanCtrl, which in turn exposes the methods of the Hello bean. Remember that the Hello bean has two methods, namely the create method to obtain a reference to a Hello bean instance, and the component method hello. When you use an EJB control, you do not have to call the create method first to obtain a reference, because the EJB control will handle this automatically for you. Instead you can simply invoke the hello method directly. To learn more about web services, see the Getting Started Tutorial: Web Services.

To Run the Test Web Service

Let's run the test web service and test the behavior of the EJBs.

  1. Make sure the HelloBeanCtrlTest Web Service is open.
  2. Click the Start button.



    Workshop launches the Workshop Test Browser.
  3. Enter your name in the String field and click the hello button.
  4. Scroll down to the Service Response section, and notice that the response is Hello, <your name>!

    In the preceding two steps the web service invoked the hello method on the Hello bean via the EJB control. The Hello bean invoked the findByPrimary method of the Visit bean to find out if your name is stored in the database. Because you are running this test for the first time, your name could not be found. A record with your name was created in the database and the Hello bean sent the appropriate response.
  5. Click the Test Operations link.
  6. Enter your name again, click the hello button, and notice that the response is Hello again, <your name>!

    In this step the Hello bean again invoked the findByPrimary method of the Visit bean to find out if your name is stored in the database. Your name was found, the database record was updated to reflect that this was your second visit, and the Hello bean sent the appropriate response.
  7. Click the Test Operations link, enter your name again, click the hello button, and notice that the response is Hello <your name>! This is visit number 3.
  8. Repeat the test with a different name and observe the outcome.
  9. Return to WebLogic Workshop and press the Stop button to close the Test Browser.

Click one of the following arrows to navigate through the tutorial: