技术开发 频道

基于Jazz技术构建企业级Web2.0应用(2)

PetStore服务的测试

    下面我们在PetStore中增加一个插件项目com.ibm.petstore.service.tests用于RPC服务和REST服务的单元测试。

    1.创建service test bundle

    File->New Plug-in Project ,给project取名为com.ibm.petstore.service.tests。

    设置目标环境为Eclipse,不创建Activator和UI,不选择Rich Client Application。

    2.插件依赖设置

    修改MANIFEST.MF,在Dependencies 选项卡中添加我们将需要的插件依赖关系,如图2所示

    · org.junit
    · com.ibm.petstore.common
    · com.ibm.petstore.service
    · com.ibm.team.repository.common
    · com.ibm.team.repository.service
    · com.ibm.team.repository.service.tests


    图2. 测试插件依赖设置

    3.编写测试代码

    编写测试代码需要针对每一个服务编写Test Case,同时提供一个Test suite用于测试执行。

    · 建立Test Case

    针对每一个RPC服务和REST服务建立相应的测试用例。值得注意的是,测试用例类的不是继承自junit.framework.TestCase,而是继承自com.ibm.team.repository.service.tests.AbstractRemoteServiceTestCase。

    AbstractRemoteServiceTestCase是Jazz服务器端测试用例的基础类,它提供了几个有用的方法,其中最重要的是getService。通过getService方法可以获取需要测试的服务接口,并通过调用服务接口的方法进行测试。

    下面以ICategoryService 服务接口为例探讨测试用例的编写,如清单15所示。

    清单15. ICategoryService服务接口的测试用例

package com.ibm.petstore.service.tests;

import com.ibm.petstore.common.ICategory;
import com.ibm.petstore.common.service.ICategoryService;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.repository.service.tests.AbstractRemoteServiceTestCase;

public class CategoryServiceTest extends AbstractRemoteServiceTestCase {

    private ICategory _tempCategory;
    public CategoryServiceTest() {
    }

    public CategoryServiceTest(String name) {
        super(name);
   }
   
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        ICategoryService service = getService();
        ICategory category = service.createCategory(12345, "small", "the small animal");
        _tempCategory = service.saveCategory(category);
    }
   
    public ICategoryService getService()
    {
        return getService(ICategoryService.class);
    }
   
    public void testFindCategoryById() throws TeamRepositoryException
    {
        ICategoryService service = getService();
        int id = _tempCategory.getId();
        ICategory category = service.findCategoryById(id);
        this.assertEquals(category.getItemId(), _tempCategory.getItemId());  
    }
     
    public void testFindAllCategories() throws Exception {
        ICategoryService service = getService();
        int nCategoryNumOld = service.findAllCategories().length;
       
        // create a new category
        ICategory category = service.createCategory(12345, "small", "the small animal");
        category = service.saveCategory(category);
        int nCategoryNumNew = service.findAllCategories().length;
       
        // the number of categories has been increased
        assertEquals(nCategoryNumOld + 1, nCategoryNumNew);
    }
    …

}

    可以看出测试过程中,首先通过getService方法获取ICategoryService服务接口,然后对ICategoryService中的方法逐一进行测试。

    值得注意的是可以在setup中做一些前期的工作,例如创建临时的Category,这样可以用于测试查询功能。

    · 建立TestSuite,如清单16所示。

    清单16. TestSuite

public class AllTests {

    public static Test suite() {
        TestSuite suite = new TestSuite(
                "Test for com.ibm.petstore.service.test");
        //$JUnit-BEGIN$
        suite.addTestSuite(CategoryServiceTest.class);
        suite.addTestSuite(ProductServiceTest.class);
        suite.addTestSuite(SellerServiceTest.class);
        suite.addTestSuite(PetStoreRestServiceTest.class);
        //$JUnit-END$
        return suite;
    }
}

    4.测试执行

    测试执行通过创建一个JUnit Plug-in Test来执行单元测试,具体步骤如下:

    1)创建Launcher

    Run->Open Run Dialog,创建一个JUnit Plugin-in Test  launcher。

    2)设置测试类

    选择项目为:com.ibm.petstore.service.tests,

    选择Test Class为:com.ibm.petstore.service.tests.AllTests

    3)设置启动模式

    使用[No Application] - Headless Mode作为要运行的应用程序

    4)设置VM参数,如清单17所示

    清单17. VM参数设置

    -Dcom.ibm.team.core.tests.repo=local -Dcom.ibm.team.repository.db.jdbc.location=${target_home}/../../server/petstore/repositoryDB

    第一个参数指定数据库使用本地模式,第二个参数标识 Jazz 存储库数据库的位置。上面显示的数据库路径应该指向缺省数据库。(这里的数据库路径与创建数据库的路径一致)。

    5)设置运行所需的Plug-ins

    选择all workspace and enabled target plug-ins通过以上设置就可以保证服务测试插件的执行了。

总结

    本文通过PetStore应用实例介绍了基于Jazz平台的服务定义、实现和测试。运用Jazz平台,可以快速地构建具有REST风格的服务,从而实现企业级的Web2.0应用。

参考资料

    · https://jazz.net/learn/LearnItem.jsp?href=content/docs/platform-overview/index.html

    · 关于Jazz REST服务请参考https://jazz.net/wiki/bin/view/Main/JazzRESTServicesMain

0
相关文章