开发者社区> 问答> 正文

HSF 单元测试该如何测试


在测试环境中,有两种方式做单元测试。
Demo 下载

方式一 、通过 LightApi 代码发布和订阅服务


  1. 在 Maven 中添加 LightApi 依赖。[backcolor=transparent] [backcolor=transparent]<dependency>
  2. [backcolor=transparent]     [backcolor=transparent]<groupId>[backcolor=transparent]com.alibaba.hsf[backcolor=transparent]</groupId>
  3. [backcolor=transparent]     [backcolor=transparent]<artifactId>[backcolor=transparent]LightApi[backcolor=transparent]</artifactId>
  4. [backcolor=transparent]     [backcolor=transparent]<version>[backcolor=transparent]1.0.0[backcolor=transparent]</version>
  5. [backcolor=transparent] [backcolor=transparent]</dependency>

创建 ServiceFactory。
这里需要设置 Pandora 的地址,参数是 SAR 包所在目录。如果 SAR 包地址是 /Users/Jason/Work/AliSoft/PandoraSar/DevSar/taobao-hsf.sar,则参数如下:
  1. [backcolor=transparent]private[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]final[backcolor=transparent] [backcolor=transparent]ServiceFactory[backcolor=transparent] factory [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]ServiceFactory[backcolor=transparent].[backcolor=transparent]getInstanceWithPath[backcolor=transparent]([backcolor=transparent]"/Users/Jason/Work/AliSoft/PandoraSar/DevSar"[backcolor=transparent]);

通过代码进行发布和订阅服务。
  1. [backcolor=transparent] [backcolor=transparent]// 进行服务发布(若有发布者,无需再这里写)
  2. [backcolor=transparent] factory[backcolor=transparent].[backcolor=transparent]provider[backcolor=transparent]([backcolor=transparent]"helloProvider"[backcolor=transparent])[backcolor=transparent]// 参数是一个标识,初始化后,下次只需调用provider("helloProvider")即可拿出对应服务
  3. [backcolor=transparent]         [backcolor=transparent].[backcolor=transparent]service[backcolor=transparent]([backcolor=transparent]"com.alibaba.edas.unit.service.UnitTestService"[backcolor=transparent])[backcolor=transparent]// 接口全类名
  4. [backcolor=transparent]         [backcolor=transparent].[backcolor=transparent]version[backcolor=transparent]([backcolor=transparent]"1.0.0"[backcolor=transparent])[backcolor=transparent]// 版本号
  5. [backcolor=transparent]         [backcolor=transparent].[backcolor=transparent]group[backcolor=transparent]([backcolor=transparent]"light"[backcolor=transparent])[backcolor=transparent]// 组别
  6. [backcolor=transparent]         [backcolor=transparent].[backcolor=transparent]impl[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]UnitTestServiceImpl[backcolor=transparent]())[backcolor=transparent]// 对应的服务实现
  7. [backcolor=transparent]         [backcolor=transparent].[backcolor=transparent]publish[backcolor=transparent]();[backcolor=transparent]// 发布服务,至少要调用service()和version()才可以发布服务
  8. [backcolor=transparent] [backcolor=transparent]// 进行服务消费
  9. [backcolor=transparent] factory[backcolor=transparent].[backcolor=transparent]consumer[backcolor=transparent]([backcolor=transparent]"helloConsumer"[backcolor=transparent])[backcolor=transparent]// 参数是一个标识,初始化后,下次只需调用consumer("helloConsumer")即可直接拿出对应服务
  10. [backcolor=transparent]         [backcolor=transparent].[backcolor=transparent]service[backcolor=transparent]([backcolor=transparent]"com.alibaba.edas.unit.service.UnitTestService"[backcolor=transparent])[backcolor=transparent]// 接口全类名
  11. [backcolor=transparent]         [backcolor=transparent].[backcolor=transparent]version[backcolor=transparent]([backcolor=transparent]"1.0.0"[backcolor=transparent])[backcolor=transparent]// 版本号
  12. [backcolor=transparent]         [backcolor=transparent].[backcolor=transparent]group[backcolor=transparent]([backcolor=transparent]"light"[backcolor=transparent])[backcolor=transparent]// 组别
  13. [backcolor=transparent]         [backcolor=transparent].[backcolor=transparent]subscribe[backcolor=transparent]();
  14. [backcolor=transparent] factory[backcolor=transparent].[backcolor=transparent]consumer[backcolor=transparent]([backcolor=transparent]"helloConsumer"[backcolor=transparent]).[backcolor=transparent]sync[backcolor=transparent]();[backcolor=transparent]// 同步等待地址推送,最多6秒。
  15. [backcolor=transparent] [backcolor=transparent]UnitTestService[backcolor=transparent] log4jService [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]UnitTestService[backcolor=transparent])[backcolor=transparent] factory[backcolor=transparent].[backcolor=transparent]consumer[backcolor=transparent]([backcolor=transparent]"helloConsumer"[backcolor=transparent]).[backcolor=transparent]subscribe[backcolor=transparent]();[backcolor=transparent]// 用ID取出对应服务,subscribe()方法返回对应的接口
  16. [backcolor=transparent] [backcolor=transparent]// 调用服务方法
  17. [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"bean -> msg rec success:-"[backcolor=transparent]+[backcolor=transparent]log4jService[backcolor=transparent].[backcolor=transparent]print[backcolor=transparent]());


方式二、通过 XML 配置发布订阅服务。


  1. 编写好 HSF 的 XML 配置。

  2. 通过代码方式加载配置文件。[backcolor=transparent] [backcolor=transparent]//XML方式加载服务提供者
  3. [backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]ClassPathXmlApplicationContext[backcolor=transparent]([backcolor=transparent]"hsf-provider-beans.xml"[backcolor=transparent]);
  4. [backcolor=transparent] [backcolor=transparent]//XML方式加载服务消费者
  5. [backcolor=transparent] [backcolor=transparent]ClassPathXmlApplicationContext[backcolor=transparent] consumerContext[backcolor=transparent]=[backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]ClassPathXmlApplicationContext[backcolor=transparent]([backcolor=transparent]"hsf-consumer-beans.xml"[backcolor=transparent]);
  6. [backcolor=transparent] [backcolor=transparent]//获取Bean
  7. [backcolor=transparent] [backcolor=transparent]UnitTestXMLConsumer[backcolor=transparent] unitTestXMLConsumer[backcolor=transparent]=([backcolor=transparent]UnitTestXMLConsumer[backcolor=transparent])[backcolor=transparent] consumerContext[backcolor=transparent].[backcolor=transparent]getBean[backcolor=transparent]([backcolor=transparent]"unitTestConsumer"[backcolor=transparent]);
  8. [backcolor=transparent] [backcolor=transparent]//服务调用
  9. [backcolor=transparent] unitTestXMLConsumer[backcolor=transparent].[backcolor=transparent]testUnitProvider[backcolor=transparent]();

展开
收起
猫饭先生 2017-10-26 10:42:22 3290 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
移动互联网测试到质量的转变 立即下载
给ITer的技术实战进阶课-阿里CIO学院独家教材(四) 立即下载
F2etest — 多浏览器兼容性测试整体解决方案 立即下载