Why is my compiler not allowing myself to subscribe on an Observable
with a TestSubscriber
?
Here's my code:
TestSubscriber<User> testSubscriber = new TestSubscriber<>(); Observable.just(new User()).subscribe(testSubscriber);
And it's saying that it can't resolve method subscribe
which is taking this parameter. But in all RxJava testing tutorials, they are using TestSubscriber
without such problems. What can I do to test such Observable
?
RxJava implements several variants of subscribe . If you pass it no parameters, it will trigger a subscription to the underlying Observable, but will ignore its emissions and notifications. This will activate a cold Observable.
Following are the convenient methods to create observables in Observable class. just(T item) − Returns an Observable that signals the given (constant reference) item and then completes. fromIterable(Iterable source) − Converts an Iterable sequence into an ObservableSource that emits the items in the sequence.
just() This is one of the easiest and convenient ways to create observable. just() constructs a reactive type by taking a pre-existing object and emitting that specific object to the downstream consumer upon subscription. The just operator converts an item into an Observable that emits that item.
It is because *Subscriber
are meant for Flowable
while Observable
uses the *Observer
classes. This is because the reactive-streams standard reserves Subscriber
for the fully compliant Publisher
interface which Flowable
implements.
Additionally with RxJava2 all reactive classes have a .test()
method which will directly give you the appropriate testing object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With