edit-icon download-icon

Take HSF unit tests

Last Updated: Nov 16, 2018

There are two unit test methods in the test environment.

Method 1: Publish and subscribe to services using the LightApi code

Method 2: Publish and subscribe to services using XML configuration

For samples, click Demo download.

Method 1: Publish and subscribe to services using the LightApi code

  1. Add LightApi dependency to Maven.

    1. <dependency>
    2. <groupId>com.alibaba.hsf</groupId>
    3. <artifactId>LightApi</artifactId>
    4. <version>1.0.0</version>
    5. </dependency>
  2. Create ServiceFactory.

    The Pandora address must be set, and the parameter indicates the directory where the SAR package is located.If the SAR package address is /Users/Jason/Work/AliSoft/PandoraSar/DevSar/taobao-hsf.sar, the parameter value is as follows:

    1. private static final ServiceFactory factory = ServiceFactory.getInstanceWithPath("/Users/Jason/Work/AliSoft/PandoraSar/DevSar");
  3. Use code to publish and subscribe to the service.

    1. // Publish the service. (This step can be skipped if a publisher exists).
    2. factory.provider("helloProvider")// The parameter is an identifier. After initialization, you can call provider("helloProvider") to obtain the corresponding service.
    3. .service("com.alibaba.edas.unit.service.UnitTestService")// Interface Fully-Qualified Class Name (FQCN)
    4. .version("1.0.0")// Version
    5. .impl(new UnitTestServiceImpl())// Corresponding service implementation
    6. .publish();// Publish the service. At least service() and version() need to be called to publish the service.
    7. // Consume the service.
    8. factory.consumer("helloConsumer")// The parameter is an identifier. After initialization, you can call consumer("helloConsumer") to obtain the corresponding service.
    9. .service("com.alibaba.edas.unit.service.UnitTestService")// Interface Fully-Qualified Class Name (FQCN)
    10. .version("1.0.0")// Version
    11. .subscribe();
    12. factory.consumer("helloConsumer").sync();// Synchronously wait for address push in 6 seconds at most.
    13. UnitTestService log4jService = (UnitTestService) factory.consumer("helloConsumer").subscribe();// Obtain the corresponding service by using the ID. The subscribe() method returns the corresponding interface.
    14. // Call the service method.
    15. System.out.println("bean -> msg rec success:-"+log4jService.print());

Method 2: Publish and subscribe to services using XML configuration

  1. Complete the XML configuration of HSF.

  2. Load the configuration file using code.

    1. //Load the service provider using XML
    2. new ClassPathXmlApplicationContext("hsf-provider-beans.xml");
    3. //Load the service consumer using XML
    4. ClassPathXmlApplicationContext consumerContext=new ClassPathXmlApplicationContext("hsf-consumer-beans.xml");
    5. //Obtain the bean
    6. UnitTestXMLConsumer unitTestXMLConsumer=(UnitTestXMLConsumer) consumerContext.getBean("unitTestConsumer");
    7. //Call the service.
    8. unitTestXMLConsumer.testUnitProvider();
Thank you! We've received your feedback.