How to do it...

To render or retrieve the emitted data, a subscription API must be created. Perform the following steps on how to implement Subscriber<T>:

  1. Create a test class TestEmployeeNativeStreamservice that will verify some of the methods in the previous EmployeeNativeStreamservice. Add the following test method that executes processFormUser() using Subscriber<T> implemented through java.util.function.Consumer<T>:
@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@ContextConfiguration(classes = {  
SpringDbConfig.class, SpringDispatcherConfig.class }) 
public class TestEmployeeNativeStreamservice { 
 
@Test 
public void testMonoUserA(){ 
 
Consumer<String> convertUser = (str) ->  
         System.out.println("String object: " + str); 
employeeNativeStreamserviceImpl 
.processFormUser("sjct").subscribe(convertUser); 
} 
} 
  1. Add another test method that generates a subscriber using a method reference in functional programming. This time, this test case aims to run the getAllAge() method:
@Test 
public void testFluxAgeArray(){ 
      List<Integer> bufferedAge = new ArrayList<>(); 
      employeeNativeStreamserviceImpl 
.getAllAge(new Integer[]{1,2,3,4}) 
.subscribe(bufferedAge::add); 
      for(Integer age: bufferedAge){ 
         System.out.println(age); 
      } 
}
  1. Another subscriber that can execute processFormUser() is instantiated using the Reactor Stream's Subscriber<T> API which is depicted in the method as follows:
@Test 
public void testMonoUserC(){ 
  Subscriber<String> subscriber = new Subscriber<String>(){ 
 
      @Override 
      public void onComplete() { 
         System.out.println("Mono Streams ended 
 successfully."); 
      } 
 
      @Override 
      public void onError(Throwable e) { 
         System.out.println("Something wrong happened.  
            Exits now.");      
} 
 
      @Override 
      public void onNext(String name) { 
         System.out.println("String object: " + name); 
      } 
 
      @Override 
      public void onSubscribe(Subscription subs) { 
         subs.request(Long.MAX_VALUE); 
      } 
}; 
 
employeeNativeStreamserviceImpl 
       .processFormUser("sjctrags").subscribe(subscriber);   
} 
The anonymous inner class is used to implement Subscriber<T>.
  1. Save the file. Execute all test cases to view all the results.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.221.144