Android JUnit test blocks endlessly when Observable is observed on AndroidSchedulers.mainThread ()
I am writing a simple test that is equivalent to:
Test fun testObservable() {
val returnedObservable = Observable.create(object : Observable.OnSubscribe<String> {
override fun call(t: Subscriber<in String>) {
t.onNext("hello")
t.onCompleted()
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
val result = returnedObservable.toBlocking().first()
assertEquals("hello", result)
}
In case the .observeOn(AndroidSchedulers.mainThread())
test blocks are infinitely on returnedObservable.toBlocking().first()
.
Is there a way to transform the observable to return a result?
returnedObservable
returned from a method call using .subscribeOn and .observeOn, so removing those parameters is not an option.
+3
source to share
1 answer
I think this is a bug mentioned here: https://github.com/ReactiveX/RxAndroid/issues/50
btw why don't you use RxKotlin ?
An example would look much better:
val returnedObservable = observable<String> { subscriber ->
subscriber.onNext("hello")
subscriber.onCompleted()
}
.subscribeOn(Schedules.io())
.observeOn(AndroidSchedulers.mainThread())
+3
source to share