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