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...