Can't subscribe on a RxJava 2 Observable with TestSubscriber
Asked Answered
F

3

24

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?

Forrer answered 6/4, 2017 at 12:40 Comment(1)
github.com/ReactiveX/RxJava/wiki/…Ayah
B
27

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.

Beano answered 6/4, 2017 at 12:48 Comment(0)
P
5

For RxJava 1 → RxJava 2 migration, I've just replaced TestSubscriber with TestObserver to make Observable#subscribe call compile and work.

Ponytail answered 21/5, 2019 at 16:6 Comment(0)
R
0

You can change Observable to Flowable if you want to test with TestSubscriber as follow:

TestSubscriber<User> testSubscriber = new TestSubscriber<>();
Flowable.just(new User()).subscribe(testSubscriber);
Ribbon answered 20/2, 2023 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.