要使用本地 JUnit 测试来测试在服务器上部署的 EJB 模块,您需要将测试配置为 EJB 模块的远程客户端。这意味着,您只能通过 EJB 模块的远程接口来访问它。
通常,某些实体 Bean 和会话 Bean 具有远程接口,可以通过这些接口为客户端提供对实体 Bean 的访问。您必须为会话 Bean 的 Bean 类生成测试,修改测试文件以引用会话 Bean,并测试它的每个业务方法。
为 EJB 模块生成测试:
protected void setUp() throws NamingException, CreateException, RemoteException { ServiceLocator sl = new ServiceLocator(); newSessionHome = (NewSessionRemoteHome)sl.getRemoteHome("ejb/NewSessionBean", NewSessionRemoteHome.class); }
try { NewSessionRemote newSession = newSessionHome.create(); assertEquals("name", newSession.getName(new Integer(1))); } catch (Exception e) { fail(e.toString()); }