To render or retrieve the emitted data, a subscription API must be created. Perform the following steps on how to implement Subscriber<T>:
- 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); } }
- 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); } }
- 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>.
- Save the file. Execute all test cases to view all the results.