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

Using the TestSubscriber class for in-depth testing

The TestSubscriber instance is a special Subscriber instance, which we can pass to the subscribe() method of any Observable instance.

We can retrieve all the received items and notifications from it. We can also look at the last thread on which the notifications have been received and the subscription state.

Let's rewrite our test using it, in order to demonstrate its capabilities and what it stores:

public void testUsingTestSubscriber() {
  TestSubscriber<String> subscriber =
    new TestSubscriber<String>();
  Assert.assertEquals(expected, subscriber.getOnNextEvents());
  Assert.assertSame(1, subscriber.getOnCompletedEvents().size());

The test is, again, very simple. We create a TestSubscriber instance and subscribe to the tested Observable instance with it. And we have access...