当前页面: 开发资料首页 → Netbeans 专题 → 使用 NetBeans IDE 测试 Enterprise Beans
摘要: 使用 NetBeans IDE 测试 Enterprise Beans John Jullion-Ceccarelli 和 Petr Blaha JUnit 并不是测试企业应用程序的最佳测试框架,因为...
John Jullion-Ceccarelli 和 Petr Blaha
JUnit 并不是测试企业应用程序的最佳测试框架,因为 JUnit 运行在与 enterprise beans 不同的 JVM 上。因此,必须使用远程接口来访问并测试 enterprise beans。
开始之前,必须在计算机上安装 Sun Java System (SJS) Application Server Platform Edition 8.1(下载)。您无法将企业应用程序部署到绑定的 Tomcat 服务器。还必须在 IDE 中注册应用程序服务器,通过选择 Tools > Server Manager。
下载示例项目 TestEnterpriseBean 并在 IDE 中打开它。该 bean 有一个会话 bean (AdminBean),拥有三个简单的业务方法:
public String getString(String name) { return "response:" + name; } public int getInt(int number1, int number2) { return number1 * number2; } public vo.Person getVO() { return new vo.Person("Joe User",13); }
private beantest.AdminRemote admin;
private beantest.AdminRemote lookupAdminBean() { try { javax.naming.Context c = new javax.naming.InitialContext(); Object remote = c.lookup("ejb/AdminBean"); beantest.AdminRemoteHome rv = (beantest.AdminRemoteHome) javax.rmi.PortableRemoteObject.narrow(remote, beantest.AdminRemoteHome.class); return rv.create(); }
protected void setUp() throws Exception { admin = lookupAdminBean(); } protected void tearDown() throws Exception { admin.remove(); }
public void testGetString() throws java.rmi.RemoteException { System.out.println("testGetString"); assertEquals("response:Test",admin.getString("Test")); } public void testGetInt() throws java.rmi.RemoteException { System.out.println("testGetInt"); assertEquals(5*3,admin.getInt(5,3)); } public void testGetVO() throws java.rmi.RemoteException { System.out.println("testGetVO"); assertEquals("xxxxx", admin.getVO().getName()); }
testGetString testGetInt testGetVO Testsuite: beantest.AdminBeanTest Tests run: 3, Failures: 1, Errors: 0, Time elapsed: 1.903 sec ------------- Standard Output --------------- testGetString testGetInt testGetVO ------------- ---------------- --------------- ------------- Standard Error ----------------- 13-Jun-2005 15:02:40 com.sun.corba.ee.spi.logging.LogWrapperBase doLog INFO: "IOP00710299: (INTERNAL) Successfully created IIOP listener on the specified host/port: all interfaces/4681" ------------- ---------------- --------------- Testcase: testGetVO(beantest.AdminBeanTest):FAILED expected:<xxxxx> but was:<Joe User> junit.framework.ComparisonFailure: expected:<xxxxx> but was:<Joe User> at beantest.AdminBeanTest.testGetVO(AdminBeanTest.java:54) 13-Jun-2005 15:02:40 com.sun.corba.ee.spi.logging.LogWrapperBase doLog INFO: "IOP00710299: (INTERNAL) Successfully created IIOP listener on the specified host/port: all interfaces/4681" at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) Test beantest.AdminBeanTest FAILED test-report: C:\new\TestEnterpriseBean\nbproject\build-impl.xml:383: Some tests failed; see details above. BUILD FAILED (total time: 4 seconds)
public void testGetVO() throws java.rmi.RemoteException{ System.out.println("testGetVO"); assertEquals("Joe User", admin.getVO().getName()); }
testGetString testGetInt testGetVO Testsuite: beantest.AdminBeanTest Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 1.993 sec ------------- Standard Output --------------- testGetString testGetInt testGetVO ------------- ---------------- --------------- ------------- Standard Error ----------------- 13-Jun-2005 15:05:06 com.sun.corba.ee.spi.logging.LogWrapperBase doLog INFO: "IOP00710299: (INTERNAL) Successfully created IIOP listener on the specified host/port: all interfaces/4686" ------------- ---------------- --------------- 13-Jun-2005 15:05:06 com.sun.corba.ee.spi.logging.LogWrapperBase doLog INFO: "IOP00710299: (INTERNAL) Successfully created IIOP listener on the specified host/port: all interfaces/4686" test-report: test: BUILD SUCCESSFUL (total time: 3 seconds)