Unit testing Google Places Autocompete API is not possible since creating fake response is not supported. Tried creating fake response with val placeResponse = Task<FindAutocompletePredictionsResponse>.
, unfortunately response creation is internal to API. Is there any way (or workaround) to accomplish this?
@Test
fun search_testSuccess() {
// AutoComplete response
val placeResponse = Task<FindAutocompletePredictionsResponse>.
val searchConstraints = SearchPlaceConstraints("Fake place")
// AutoComplete request
val request = FindAutocompletePredictionsRequest.builder()
.setSessionToken(autocompleteSessionToken)
.setQuery(searchConstraints.queryText)
.build()
// return provided placesResponse if `PlacesClient.findAutocompletePredictions()` is called
coEvery { placesClient.findAutocompletePredictions(request) } returns placeResponse
// Result of method being tested
lateinit var result: Result<PlaceData<List<AutocompletePrediction>>>
coJustRun { result = googlePlaceSearchRemoteDataSource.search(searchConstraints)}
// Verify PlaceClient has been called
coVerify(exactly = 1) { placesClient.findAutocompletePredictions(request) }
// Assert success
assert(result is Result.Success)
}
Source: Android Questions