Rxjava: 2.x TestObserver and TestSubscriber for unit testing

Created on 5 Sep 2016  路  7Comments  路  Source: ReactiveX/RxJava

In 1.x I was using TestSubscriber to perform assertions in unit tests. But it seems that in 2.x is TestObserver the class that we need to use for this matter. Is that correct? And if that's correct, when I should use TestSubscriber?

Thanks.

Question

Most helpful comment

You use TestSubscriber with Flowable and TestObserver with Observable. You can test Single, Completable and Maybe by converting them to either Flowable or Observable, but the built-in test()s for these three return TestObserver.

All 7 comments

All reactive types also have a built in test method that can be used.

Very handy. Thanks @vanniktech

You use TestSubscriber with Flowable and TestObserver with Observable. You can test Single, Completable and Maybe by converting them to either Flowable or Observable, but the built-in test()s for these three return TestObserver.

Thanks @akarnokd

In 2.x @akarnokd mentioned that the built-in return for Single, Completable and Maybe is TestSubscriber but it looks like it actual default for Completable is now TestObserver

    @CheckReturnValue
    @SchedulerSupport(SchedulerSupport.NONE)
    public final TestObserver<Void> test() {
        TestObserver<Void> ts = new TestObserver<Void>();
        subscribe(ts);
        return ts;
    }

@davidargylethacker Thanks, edited my comment to define the correct type.

Thanks @akarnokd

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jaap-van-Hengstum picture Jaap-van-Hengstum  路  3Comments

dzharikhin picture dzharikhin  路  4Comments

dsvoronin picture dsvoronin  路  4Comments

ZakTaccardi picture ZakTaccardi  路  3Comments

nltran picture nltran  路  4Comments