[英]Unit Test for retrofit
I am learning how to use Unit Testing.我正在学习如何使用单元测试。 I have an Retrofit call which I wish to test if it returns non empty list or I also can compare the generated URL to my URL for test.我有一个 Retrofit 调用,我希望测试它是否返回非空列表,或者我也可以将生成的 URL 与我的 URL 进行比较以进行测试。 It does not matter.没关系。 But it does not work when I run it it never goes to "handleResponse" function.但是当我运行它时它不起作用,它永远不会进入“handleResponse”功能。 In my activity it works fine.在我的活动中它工作正常。
@RunWith(AndroidJUnit4::class)
class ApiTest {
@Test
fun apiConnection() {
val compositeDisposable = CompositeDisposable()
compositeDisposable.add(
ApiClient.getClient.getQuestions(Params.getParamsSearch())
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(this::handleResponse)
)
}
private fun handleResponse(objectsQueryResult: ObjectsQueryResult) {
assertTrue(objectsQueryResult.items.isEmpty());
assertEquals(objectsQueryResult.items, "");
}
}
You can use the blockingSubscribe() function for this, for example:您可以为此使用blockingSubscribe() 函数,例如:
Observable.fromCallable {
"Some data"
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.blockingSubscribe(this::handleResponse)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.