How do you properly test an Http request/response with Retrofit and Coroutines?
I'm using the latest Retrofit version: 2.6.0 which has the support on the suspend functions.
Edit
I'm also providing some code for easy implementation:
ApiInterface
@GET
suspend fun getCountriesListAsync(@Url url: String): Response<ArrayList<Country>>
Some Repository
suspend fun makeCountryApiCallAsync(): Response<ArrayList<Country>> =
treasureApi.getCountriesListAsync(COUNTRIES_API_URL)
Implementation on the ViewModel:
private suspend fun makeCountriesApiCall() {
withContext(Dispatchers.Main) {
switchProgressBarOn()
}
try {
val countriesListResponse = setupRepository.makeCountryApiCallAsync()
if (countriesListResponse.isSuccessful) {
withContext(Dispatchers.Main) {
switchProgressOff()
countriesList.value = countriesListResponse.body()
}
} else {
withContext(Dispatchers.Main) {
showErrorLayout()
}
}
} catch (e: Exception) {
e.printStackTrace()
withContext(Dispatchers.Main) {
showErrorLayout()
}
}
}