Skip Headers
Oracle® SOA Suite Developer's Guide
10g (10.1.3.1.0)

Part Number B28764-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

4.8 Testing EJBs

There are a few ways you can test your EJBs both inside and outside the container, depending on what you want to accomplish. The easiest way to test your EJBs is to create a Java service facade. A Java service facade can run outside the Java EE container, so you will not need to deploy the application or create a client. For testing inside the EE container, you can use JDeveloper's embedded server or you can deploy to an application server and test it remotely.

4.8.1 Testing Entities Outside the Container with a Java Service Facade

The standard method for testing your EJB services is to first deploy your application to an application sever, and then create a client to call methods through the session facade. However, you do not have to deploy an application to test your EJBs, you can do so within JDeveloper by creating a Java service facade. A Java service facade is a plain Java class with facade methods (CRUD methods and finder methods on entities accessed through the entity manager) and a runnable main() method. In order to test EJBs outside the container, you will also need to configure a persistence unit to run outside the container.

To test EJBs outside the container:

  1. Create a Persistence Unit that Runs Outside the Container.

  2. Create a Java Service Facade.

  3. Run the Java Service Facade.

4.8.1.1 Create a Persistence Unit that Runs Outside the Container

The first step to testing your EJBs outside the container is to create a new persistence unit that is configured to run outside the container.

To create a a persistence unit that runs outside the container:

  1. In the Navigator, right click the persistence.xml file and choose New Persistence Unit.

  2. In the Create Persistence Unit dialog, name your persistence unit so that it will be easy to identify.


    Tip:

    A persistence unit stores information about the service and connection, so naming your persistence unit something like "CusomterService-Outside" is often helpful.

  3. Select the checkbox for Outside Java EE Container.

  4. Click OK to save your changes.

Example 4-11shows the persistence.xml file from the Customer Service sample application, with a new persistence unit created to run outside the container:

Example 4-11 A persistence.xml file with a persistence unit that runs outside the container.

<?xml version="1.0" encoding="windows-1252" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="CustomerService"/>
  <persistence-unit name="CustomerService-Outside">
    <class>org.soademo.customerservice.persistence.Address</class>
    <class>org.soademo.customerservice.persistence.Customer</class>
    <properties>
      <property name="toplink.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
      <property name="toplink.jdbc.url"
                value="jdbc:oracle:thin:@Database_name:Port:SID_value"/>
      <property name="toplink.jdbc.user" value="SOADEMO"/>
      <property name="toplink.jdbc.password"
                value="A0D145248EE31051EAA1C822B80C610A"/>
      <property name="toplink.target-database" value="Oracle"/>
      <property name="toplink.logging.level" value="FINER"/>
    </properties>
  </persistence-unit>
</persistence>

4.8.1.2 Create a Java Service Facade

To create a Java service facade, you will use the Create Java Service Facade wizard.

To create a Java service facade:

  1. In the Navigator, right click the persistence.xml file and choose New Java Service Facade.

  2. On step 1 of the wizard, in the Persistence Unit list, make sure to select the persistence unit you created that runs outside the container, and select the checkbox for Generate a main() method with sample code.

  3. On step 2, use the tree control to select the service facade methods you want to test and then click Finish.


Tip:

Each time you create a Java service facade, JDeveloper will create a new service facade with the name JavaServiceFacade. If you want to save the service facade, rename it each time you generate it.

4.8.1.3 Run the Java Service Facade

The final step is to add code to the main() method and run the service facade.

To run the Java service facade:

  1. In the Navigator, double click the Java service facade class to open it in the Java Source Editor.

  2. In the Editor, scroll to the main() method and create a new method that calls any of the session facade methods you generated in Section 4.8.1.2, "Create a Java Service Facade".

  3. Right-click the file in the Editor and choose Run.

For example, the following code is the main() method from a Java service facade that has been modified to print a list of all customer email addresses.

Example 4-12 Java service facade with method that prints email addresses

public static void main(String [] args) {
   final JavaServiceFacade javaServiceFacade = new JavaServiceFacade();
   //  TODO:  Call methods on javaServiceFacade here...
   List<Customer> customerList = javaServiceFacade.queryCustomerFindAllCustomer();
      for (Customer cust : customerList) {
         System.out.println(cust.getEmail());
      }
   }

4.8.2 Testing Inside the EE Container Using the Embedded Server

JDeveloper has an embedded server that simplifies testing your EJBs. You do not have to deploy your application remotely to test your session beans.

To test EJBs using the embedded server:

  1. Create a Sample Java Client.

  2. Run the Service and Client.

4.8.2.1 Create a Sample Java Client

The easiest way to test your EJBs is to create a Java client and call methods on it. JDeveloper has a Sample Client utility that will generate a Java client and facade methods.

To create a sample Java client:

  1. In the Navigator, right-click a session bean and choose New Sample Java Client.

  2. In the Create Sample Java Client dialog, make sure Connect to OC4J Embedded in JDeveloper is selected and click OK.

  3. In the Java Source Editor, uncomment the methods you want to test or create your own.

4.8.2.2 Run the Service and Client

You must first start the service by running the session bean, then you can run the client.

To run the service and client:

  1. In the Navigator, right-click the session bean and choose Run. This may take a moment to initialize.

  2. In the Navigator, right-click the sample Java client and choose Run.

You should see output in the Log window as your client runs.