在测试环境中,有两种方式做单元测试。
Demo 下载
方式一 、通过 LightApi 代码发布和订阅服务
在 Maven 中添加 LightApi 依赖。[backcolor=transparent] [backcolor=transparent]<dependency>- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]com.alibaba.hsf[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]LightApi[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]<version>[backcolor=transparent]1.0.0[backcolor=transparent]</version>
- [backcolor=transparent] [backcolor=transparent]</dependency>
创建 ServiceFactory。
这里需要设置 Pandora 的地址,参数是 SAR 包所在目录。如果 SAR 包地址是
/Users/Jason/Work/AliSoft/PandoraSar/DevSar/taobao-hsf.sar,则参数如下:
- [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]);
通过代码进行发布和订阅服务。
- [backcolor=transparent] [backcolor=transparent]// 进行服务发布(若有发布者,无需再这里写)
- [backcolor=transparent] factory[backcolor=transparent].[backcolor=transparent]provider[backcolor=transparent]([backcolor=transparent]"helloProvider"[backcolor=transparent])[backcolor=transparent]// 参数是一个标识,初始化后,下次只需调用provider("helloProvider")即可拿出对应服务
- [backcolor=transparent] [backcolor=transparent].[backcolor=transparent]service[backcolor=transparent]([backcolor=transparent]"com.alibaba.edas.unit.service.UnitTestService"[backcolor=transparent])[backcolor=transparent]// 接口全类名
- [backcolor=transparent] [backcolor=transparent].[backcolor=transparent]version[backcolor=transparent]([backcolor=transparent]"1.0.0"[backcolor=transparent])[backcolor=transparent]// 版本号
- [backcolor=transparent] [backcolor=transparent].[backcolor=transparent]group[backcolor=transparent]([backcolor=transparent]"light"[backcolor=transparent])[backcolor=transparent]// 组别
- [backcolor=transparent] [backcolor=transparent].[backcolor=transparent]impl[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]UnitTestServiceImpl[backcolor=transparent]())[backcolor=transparent]// 对应的服务实现
- [backcolor=transparent] [backcolor=transparent].[backcolor=transparent]publish[backcolor=transparent]();[backcolor=transparent]// 发布服务,至少要调用service()和version()才可以发布服务
- [backcolor=transparent] [backcolor=transparent]// 进行服务消费
- [backcolor=transparent] factory[backcolor=transparent].[backcolor=transparent]consumer[backcolor=transparent]([backcolor=transparent]"helloConsumer"[backcolor=transparent])[backcolor=transparent]// 参数是一个标识,初始化后,下次只需调用consumer("helloConsumer")即可直接拿出对应服务
- [backcolor=transparent] [backcolor=transparent].[backcolor=transparent]service[backcolor=transparent]([backcolor=transparent]"com.alibaba.edas.unit.service.UnitTestService"[backcolor=transparent])[backcolor=transparent]// 接口全类名
- [backcolor=transparent] [backcolor=transparent].[backcolor=transparent]version[backcolor=transparent]([backcolor=transparent]"1.0.0"[backcolor=transparent])[backcolor=transparent]// 版本号
- [backcolor=transparent] [backcolor=transparent].[backcolor=transparent]group[backcolor=transparent]([backcolor=transparent]"light"[backcolor=transparent])[backcolor=transparent]// 组别
- [backcolor=transparent] [backcolor=transparent].[backcolor=transparent]subscribe[backcolor=transparent]();
- [backcolor=transparent] factory[backcolor=transparent].[backcolor=transparent]consumer[backcolor=transparent]([backcolor=transparent]"helloConsumer"[backcolor=transparent]).[backcolor=transparent]sync[backcolor=transparent]();[backcolor=transparent]// 同步等待地址推送,最多6秒。
- [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()方法返回对应的接口
- [backcolor=transparent] [backcolor=transparent]// 调用服务方法
- [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 配置发布订阅服务。
编写好 HSF 的 XML 配置。
通过代码方式加载配置文件。[backcolor=transparent] [backcolor=transparent]//XML方式加载服务提供者- [backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]ClassPathXmlApplicationContext[backcolor=transparent]([backcolor=transparent]"hsf-provider-beans.xml"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]//XML方式加载服务消费者
- [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]);
- [backcolor=transparent] [backcolor=transparent]//获取Bean
- [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]);
- [backcolor=transparent] [backcolor=transparent]//服务调用
- [backcolor=transparent] unitTestXMLConsumer[backcolor=transparent].[backcolor=transparent]testUnitProvider[backcolor=transparent]();