Book Image

Learning Reactive Programming with Java 8

By : Nickolay Tzvetinov
Book Image

Learning Reactive Programming with Java 8

By: Nickolay Tzvetinov

Overview of this book

Table of Contents (15 chapters)
Learning Reactive Programming with Java 8
About the Author
About the Reviewers

Testing using simple subscription

We can test what we get by simply subscribing to the source Observable instance and collecting all of the incoming notifications. In order to demonstrate that, we'll develop a factory method for creating a new Observable instance and will test its behavior.

The method will receive a Comparator instance and multiple items, and will return Observable instance, emitting these items as a sorted sequence. The items will be sorted according to the Comparator instance passed.

We can develop the method using TDD. Let's first define the test as follows:

public class SortedObservableTest {
  private Observable<String> tested;
  private List<String> expected;
  public void before() {
    tested = CreateObservable.<String>sorted(
      (a, b) -> a.compareTo(b),
      "Star", "Bar", "Car", "War", "Far", "Jar");
    expected = Arrays.asList(
      "Bar", "Car", "Far", "Jar", "Star", "War"
  TestData data = new TestData();