edit-icon download-icon

Take HSF unit tests

Last Updated: Jan 16, 2018

There are two unit test methods in the test environment.

Demo download

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

  1. Add LightApi dependency to Maven.

     <dependency>
         <groupId>com.alibaba.hsf</groupId>
         <artifactId>LightApi</artifactId>
         <version>1.0.0</version>
     </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 , then the parameter value is as follows:

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

     // Publish the service. (This step can be skipped if a publisher exists).
     factory.provider("helloProvider")// The parameter is an identifier. After initialization, you can call provider("helloProvider") to obtain the corresponding service.
             .service("com.alibaba.edas.unit.service.UnitTestService")// Interface Fully-Qualified Class Name (FQCN)
             .version("1.0.0")// Version
             .impl(new UnitTestServiceImpl())// Corresponding service implementation
             .publish();// Publish the service. Need to call service() and version() at least to publish the service.
    
     // Consume the service.
     factory.consumer("helloConsumer")// The parameter is an identifier. After initialization, you can call consumer("helloConsumer") to obtain the corresponding service.
             .service("com.alibaba.edas.unit.service.UnitTestService")// Interface Fully-Qualified Class Name (FQCN)
             .version("1.0.0")// Version
             .subscribe();
     factory.consumer("helloConsumer").sync();// Synchronously wait for address push in 6 seconds at most.
     UnitTestService log4jService = (UnitTestService) factory.consumer("helloConsumer").subscribe();// Obtain the corresponding service through the ID. The subscribe() method returns the corresponding interface.
     // Call the service method.
     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.

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