Testing an EJB Module

See Also

In order to use local JUnit tests to test an EJB module that is deployed on a server, you need to configure the tests to act as a remote client of the EJB module. This means you can only access the EJB module through its remote interfaces.

Usually, you have some entity beans and a session bean with remote interfaces that provides clients access to the entity beans. You have to generate tests for the session bean's bean class and modify the test file to reference the session bean and test each of its business methods.

To generate tests for an EJB module:

  1. Create a service locator file somewhere in the test package source root.
  2. Open the enterprise bean's bean class in the Source Editor and choose Tools > JUnit Tests > Create Tests (Ctrl-Alt-J). Accept the default options in the dialog box and click OK.
  3. In the test class, delete the test methods that just test EJB infrastructure methods such as testEjbCreate and testEjbRemove.
  4. Declare a variable for the remote home interface.
  5. In the setUp method, write code to instantiate this variable, as in the following example:
    protected void setUp() throws NamingException, 
    			CreateException, RemoteException {
        ServiceLocator sl = new ServiceLocator();
        newSessionHome = (NewSessionRemoteHome)sl.getRemoteHome("ejb/NewSessionBean", 
    	        NewSessionRemoteHome.class);
    }
  6. In each test method, retrieve a remote interface and test the business method, as in the following example:
    try {
        NewSessionRemote newSession = newSessionHome.create();
        assertEquals("name", newSession.getName(new Integer(1)));
    }
    catch (Exception e) {
        fail(e.toString());
    }
See Also
About JUnit
Running JUnit Tests
Configuring JUnit
About Debugging and Testing Web Applications
Using a Service Locator

Legal Notices