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
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

@Test
public void testUsingTestSubscriber() {
  TestSubscriber<String> subscriber =
    new TestSubscriber<String>();
  tested.subscribe(subscriber);
  Assert.assertEquals(expected, subscriber.getOnNextEvents());
  Assert.assertSame(1, subscriber.getOnCompletedEvents().size());
  Assert.assertTrue(subscriber.getOnErrorEvents().isEmpty());
  Assert.assertTrue(subscriber.isUnsubscribed());
}

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