mocking Retrofit response calls with Call not working mocking Retrofit response calls with Call not working android android

mocking Retrofit response calls with Call not working


Call is an interface, you can create an object that implements it and return it from your mocking method:

class ApiServiceMock : ApiService {    override fun getStep1User(): Call<UserResponse> {        return object: Call<SignedUserUi> {            override fun enqueue(callback: Callback<UserResponse>?) {            }            override fun isExecuted(): Boolean {                return false            }            override fun clone(): Call<UserResponse> {                return this            }            override fun isCanceled(): Boolean {                return false            }            override fun cancel() {            }            override fun request(): Request {                return Request.Builder().build()            }            override fun execute(): Response<UserResponse> {                // Create your mock data in here                val response = "{ \"Response\": \"SomeUserValue\" }"                val gson = Gson().toJson(response)                return Response.success(UserResponse(gson))            }        }    }}

If you want to have less boilerplate and be able to mock interfaces in a single line I would recommend you to take a look at mockito for kotlin.

After including it to your project you'll be able to do

val rawResponse = "{ \"Response\": \"SomeUserValue\" }"val gson = Gson().toJson(rawResponse)val response = Response.success(UserResponse(gson))val mockCall = mock<Call<UserResponse>> {    on { execute() } doReturn response}val mockApiService = mock<ApiService> {    on { getStep1User() } doReturn mockCall}


What you are trying to do is testin retrofit !You must assert on the behavior of your application after getting the response, not asserting on what retrofit get as response of the request!!For example assert that an error response, an error dialog must appear.

You can mock the response using OkHTTPMock server.add the dependency in your build.gradle module file:

testImplementation 'com.squareup.okhttp3:mockwebserver:lastVersion'

and then in your test file you can mock a server, a request and a response.here an example:

MockWebServer server = new MockWebServer();server.enqueue(new MockResponse().setBody("{ \"Response\": \"SomeUserValue\" }"));// Start the server.  server.start();//and than load your request. Be Careful, they are executed in the order that you enqued them!//Add your assertion (Succes response and error response)//if you are working with MVP architecture, you can assert for the success case//that method showData(data) is called using Mockito.verify(myPresenter).showData(data);

Take a look at the officiel example of OkHttpMock


You can use RETURNS_DEEP_STUBS. But not sure how does it work in Kotlin.

mock = Mockito.mock(Api.class, RETURNS_DEEP_STUBS)when(mock.getSomething().execute()).thenReturn(Response.success(...));