Testing with the aggregate operators and the BlockingObservable class

Using the operators and methods learned in the previous two sections, we are able to rework the test we've written to look like this:

@Test
public void testUsingBlockingObservable() {
  List<String> result = tested
    .toList()
    .toBlocking()
    .single();
  Assert.assertEquals(expected, result);
}

There is no boilerplate code here. We retrieve all the items emitted as a list and compare them to the expected list.

Using the BlockingObsevables class and the aggregate operators is pretty useful in most cases. While testing asynchronous Observable instances, which emit long, slow sequences, they are not so useful though. It is not good practice to block the test cases for a long time: slow tests are bad tests.

Note

The source code for the preceding test can be found at https://github.com/meddle0x53/learning-rxjava/blob/master/src/test/java/com/packtpub/reactive/chapter07/SortedObservableTest.java—this is the second test method.

Another case in which this method of testing is not helpful is when we want to inspect the Notification objects sent by the source or the subscription state.

There is one other technique for writing tests that gives us more fine-grained control over the subscription itself, and this is via a special Subscriber—the TestSubscriber.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.47.169