公司的产品要引入单元测试以及代码覆盖,让我做一个框架方便我们写单元测试代码,上网找了一下,找到一个不错的基础:MockEJB,大家可以先了解下相关的内容:
MockEJB项目简介信息
使用MockEJB简化EJB测试

我在这个基础上进行了扩充,框架具有如下的特性:

  • 读取EJB配制文件完成EJB的自动发布,框架会读取我们工程的ejb-jar.xml,jboss.xml,jbosscmp- jdbc.xml文件的配制信息并组合这些信息完成自动发布所需要的内容。
  • 共享的dummy数据,我们自己提供一个xml文件,里面存放一些dummy的数据,这些数据会在对应的Entity Bean被使用的时候自动插入数据库,完成数据的初始化工作(这样Session Bean中的SQL也可以取到一致的内容),在一个test方法结束的时候回滚,删除那些记录,消除Test Case之间的影响。
  • 自定义事务管理,每个test方法都处于同一个事务中,方法结束的时候事务回滚,消除Test Case之间的影响。
  • 支持部分find方法的实现,由于MockEJB对于Entity Bean只提供了对findByPrimaryKey的支持,甚至连findAll都不支持,框架在MockEJB提供的Aspect机制的基础上实现了 findAll等常用必备的find方法,并且对于自定义的find方法也提供部分支持,通过解析EJB配制文件中的SQL完成,但是可能性能不好,因为会遍历所有的记录并比较字段的值完成这个查找的过程。其它比较复杂的find方法(框架无法解析的SQL),需要自己实现对应的Aspect,框架会自动查找对应的Aspect。
  • Struts TestCase的基础上支持对Action的单元测试。

测试代码范例:
测试Action
package com.xxx.test.action;

import java.util.List;

import com.xxx.controller.util.Team;
import com.xxx.test.base.BaseActionTestCase;

public class TestListAllPersonsAction extends BaseActionTestCase {
  public TestListAllPersonsAction(String testName) {
    super(testName);
  }

  public void testSuccessful() throws Exception {
    setRequestPathInfo("/ListAllPersons.do");
    actionPerform();
    verifyForward("success");
    List persons = (List) getRequest().getAttribute("PERSON_LIST");
    assertEquals(9, persons.size());
    Team team = (Team) getRequest().getAttribute("TEAM");
    assertNotNull(team);
  }

}

测试Session Bean:
package com.xxx.test.ejb;

import java.util.ArrayList;
import java.util.List;

import com.xxx.common.data.CrystalReportConfigValueObj;
import com.xxx.common.data.attr.CrystalReportConfigAttributes;
import com.xxx.common.exception.ValidationException;
import com.xxx.common.util.ejb.EjbServerFactory;
import com.xxx.controller.server.AppServerFactory;
import com.xxx.persistance.CrystalReportConfig;
import com.xxx.persistance.CrystalReportConfigHome;
import com.xxx.persistance.CrystalReportExecStateHome;
import com.xxx.persistance.CrystalReportInstParamConfigHome;
import com.xxx.persistance.CrystalReportInstanceConfigHome;
import com.xxx.persistance.CrystalReportParamConfigHome;
import com.xxx.persistance.CrystalReportPersonExcludeHome;
import com.xxx.persistance.CrystalReportScheduleHome;
import com.xxx.persistance.CrystalReportSubscriberConfigHome;
import com.xxx.session.infrastructure.CrystalReportConfigOperation;
import com.xxx.session.infrastructure.CrystalReportConfigOperationHome;
import com.xxx.test.base.BaseEJBTestCase;

public class TestCrystalReportConfigOperation extends BaseEJBTestCase {
  CrystalReportConfigHome crystalReportConfigHome;

  CrystalReportConfigOperationHome crystalReportConfigOperationHome;

  CrystalReportConfigOperation crystalReportConfigOperation;

  public TestCrystalReportConfigOperation(String name) {
    super(name);
  }

  public void setUp() throws Exception {
    super.setUp();
    crystalReportConfigHome = (CrystalReportConfigHome) lookup(CrystalReportConfigHome.JNDI_NAME);
    crystalReportConfigOperationHome = (CrystalReportConfigOperationHome) lookup(CrystalReportConfigOperationHome.JNDI_NAME);
    crystalReportConfigOperation = (CrystalReportConfigOperation) AppServerFactory
        .getAppServerLocator().createRemoteObject(
            CrystalReportConfigOperationHome.JNDI_NAME,
            CrystalReportConfigOperationHome.class);
  }

  public void testGetCrystalReportConfig() throws Exception {
    CrystalReportConfigValueObj vo = crystalReportConfigOperation
        .getCrystalReportConfig(getUserSession(), new Long(1));
    assertNotNull(vo);
    assertEquals("1-Change Summary", vo
        .getAttribute(CrystalReportConfigAttributes.reportName));
    assertEquals(new Long(2554), vo
        .getAttribute(CrystalReportConfigAttributes.serverId));
    assertTrue(((Boolean) vo
        .getAttribute(CrystalReportConfigAttributes.hasParams)).booleanValue());
    assertEquals(2, vo.getParameters().size());
    vo = crystalReportConfigOperation.getCrystalReportConfig(getUserSession(),
        new Long(2));
    assertNotNull(vo);
    assertEquals("2-Managed Servers", vo
        .getAttribute(CrystalReportConfigAttributes.reportName));
    assertEquals(new Long(2556), vo
        .getAttribute(CrystalReportConfigAttributes.serverId));
    assertTrue(((Boolean) vo
        .getAttribute(CrystalReportConfigAttributes.hasParams)).booleanValue());
    assertEquals(4, vo.getParameters().size());
    vo = crystalReportConfigOperation.getCrystalReportConfig(getUserSession(),
        new Long(3));
    assertNotNull(vo);
    assertEquals("3-Server Change Summary", vo
        .getAttribute(CrystalReportConfigAttributes.reportName));
    assertEquals(new Long(2553), vo
        .getAttribute(CrystalReportConfigAttributes.serverId));
    assertTrue(((Boolean) vo
        .getAttribute(CrystalReportConfigAttributes.hasParams)).booleanValue());
    assertEquals(6, vo.getParameters().size());
  }

  public void testSynchronizeConfigWithCrystalReportServer() throws Exception {

  }

  public void testGetAll() throws Exception {
    List serverReports = (List) crystalReportConfigHome.findAll();
    List crserverReports = new ArrayList();
    for (int i = 0; i < serverReports.size(); i++) {
      crserverReports.add(crystalReportConfigOperation.getCrystalReportConfig(
          getUserSession(), ((CrystalReportConfig) serverReports.get(i))
              .getId()));
    }
    List result = crystalReportConfigOperation.getAll(getUserSession(),
        crserverReports);
    assertEquals(5, result.size());
  }

  public void testClear() throws Exception {
    crystalReportConfigOperation.clear();
    assertEquals(0, ((CrystalReportScheduleHome) EjbServerFactory
        .getEJBServerLocator().findLocalEJBHome(
            CrystalReportScheduleHome.JNDI_NAME)).findAll().size());
    assertEquals(0, ((CrystalReportExecStateHome) EjbServerFactory
        .getEJBServerLocator().findLocalEJBHome(
            CrystalReportExecStateHome.JNDI_NAME)).findAll().size());
    assertEquals(0, ((CrystalReportPersonExcludeHome) EjbServerFactory
        .getEJBServerLocator().findLocalEJBHome(
            CrystalReportPersonExcludeHome.JNDI_NAME)).findAll().size());
    assertEquals(0, ((CrystalReportSubscriberConfigHome) EjbServerFactory
        .getEJBServerLocator().findLocalEJBHome(
            CrystalReportSubscriberConfigHome.JNDI_NAME)).findAll().size());
    assertEquals(0, ((CrystalReportInstParamConfigHome) EjbServerFactory
        .getEJBServerLocator().findLocalEJBHome(
            CrystalReportInstParamConfigHome.JNDI_NAME)).findAll().size());
    assertEquals(0, ((CrystalReportInstanceConfigHome) EjbServerFactory
        .getEJBServerLocator().findLocalEJBHome(
            CrystalReportInstanceConfigHome.JNDI_NAME)).findAll().size());
    assertEquals(0, ((CrystalReportParamConfigHome) EjbServerFactory
        .getEJBServerLocator().findLocalEJBHome(
            CrystalReportParamConfigHome.JNDI_NAME)).findAll().size());
    assertEquals(0, crystalReportConfigHome.findAll().size());
  }

  public void testGetAllDeployed() throws Exception {
    List result = crystalReportConfigOperation.getAllDeployed(getUserSession());
    assertEquals(3, result.size());
  }

  public void testSaveCrystalReportConfig() throws Exception {
    CrystalReportConfig report = crystalReportConfigHome
        .findByPrimaryKey(new Long(1));
    report.setInstances(((CrystalReportInstanceConfigHome) EjbServerFactory
        .getEJBServerLocator().findLocalEJBHome(
            CrystalReportInstanceConfigHome.JNDI_NAME))
        .findByReport(new Long(1)));
    CrystalReportConfigValueObj reportVo = crystalReportConfigOperation
        .getCrystalReportConfig(getUserSession(), new Long(1));
    reportVo.setAttribute(CrystalReportConfigAttributes.deployedDate, null);
    boolean exception = false;
    try {
      crystalReportConfigOperation.saveCrystalReportConfig(getUserSession(),
          reportVo);
    } catch (ValidationException e) {
      exception = true;
    }
    if (!exception) {
      fail();
    }
  }

}

(Visited 119 times, 1 visits today)