Dim
Dim

Reputation: 4807

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. It does not matter. But it does not work when I run it it never goes to "handleResponse" function. 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, "");

    }
}

Upvotes: 0

Views: 194

Answers (1)

southerton81
southerton81

Reputation: 1109

You can use the blockingSubscribe() function for this, for example:

  Observable.fromCallable {
                    "Some data"
                }
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .blockingSubscribe(this::handleResponse)

Upvotes: 1

Related Questions