Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't subscribe on a RxJava 2 Observable with TestSubscriber

Tags:

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?

like image 864
WWJD Avatar asked Apr 06 '17 12:04

WWJD


People also ask

What is subscribe in RxJava?

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.

How do you make an Observable in RxJava?

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.

Can we create our own Observable in RxJava?

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.


1 Answers

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.

like image 132
Kiskae Avatar answered Oct 27 '22 11:10

Kiskae