站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题Testing Enterprise Beans in NetBeans IDE

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.

Setting up the Example Project

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);
                }

Generating the Tests

  1. Right-click the project's Test Libraries node and choose Add JAR/Folder to add your application server's lib/j2ee.jar and lib/appserv-rt.jar files to the test classpath.
  2. Expand the Enterprise Beans node and double-click AdminSB to open AdminBean.java in the Source Editor.
  3. Press Ctrl-Alt-J to generate the tests. Click OK to generate the test with the default options.
  4. Delete the tests for the infrastructure methods:
    • testSetSessionContext
    • testEjbActivate
    • testEjbPassivate
    • testEjbRemove
    • testEjbCreate
  5. Add the following field declaration to AdminBeanTest:
    private beantest.AdminRemote admin;
  6. Right-click in AdminBeanTest.java and choose Enterprise Resources > Call Enterprise Bean. Select AdminSB and click OK. The IDE enters a lookupAdminBean method.
  7. Change the lookupAdminBean method to use the global JNDI name instead of the local name:
      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();
                        }
  8. Add the following code in bold to the setUp and tearDown methods:
        protected void setUp() throws Exception {
                        admin = lookupAdminBean();
                        }
    
                        protected void tearDown() throws Exception {
                        admin.remove();
                        }
  9. Implement the methods that test the enterprise bean's business methods:
        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());
                            }
                        

Running the Tests

  1. Right-click the TestEnterpriseBean project and choose Deploy Project.
  2. Choose Run > Test "TestEnterpriseBean" (Alt-F6). The IDE runs the JUnit tests and displays the following output:
    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)
  3. Now let's change the test to look for the correct person. Go to the testGetVO method in TestAdminBean.java and change make the following changes:
        public void testGetVO() throws java.rmi.RemoteException{
                        System.out.println("testGetVO");
                        assertEquals("Joe User", admin.getVO().getName());
                        }
  4. Press Alt-F6 again to run the tests. The IDE displays the following output:
    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)

↑返回目录
前一篇: Subversion Support in NetBeans IDE
后一篇: Tuning JVM switches for performance