当前页面: 开发资料首页 → Netbeans 专题 → Testing Enterprise Beans in NetBeans IDE
摘要: JUnit is not the best testing framework for testing enterprise applications because JUnit runs on a different JVM than your enterprise beans. Therefore, you have to use remote interface to access and test the enterprise beans.
Before you begin, you have to install Sun Java System (SJS) Application Server Platform Edition 8.1 (download) on your computer. You cannot deploy enterprise applications to the bundled Tomcat server. You also have to register the application server in the IDE by choosing Tools > Server Manager.
Download and the sample TestEnterpriseBean project and open it in the IDE. The bean has one session bean (AdminBean) with three simple business methods:
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)