I'm trying to use Mockito to mock out the Call\ Is it possible to just remove final on Response.class? Jersey used to have a similar final on one their classes (an intermediate object in the .post().entity().etc() chain) and removed it to help mocking.
Response is a value type. Why do you need to mock it? Just use the static
factory methods to create your own instance.
On Fri, Nov 18, 2016, 5:34 PM Chris Kessel [email protected] wrote:
I'm trying to use Mockito to mock out the Call object my retrofit2
interface is using, but I can't mock the Response returned by
call.execute() because it's final. I interrogate the response for more than
the body, I also want to mock the response.code() http status code.There's MockRetrofit, but the examples seem to require quite a lot of code
to setup and often involve creating custom mock classes.Is it possible to just remove final on Response.class? Jersey used to have
a similar final on one their classes (an intermediate object in the
.post().entity().etc() chain) and removed it to help mocking.—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/retrofit/issues/2089, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEEeP6Ell1FEkt3apTIt-dPFf2yhT7ks5q_if_gaJpZM4K2_y2
.
Because I ran into issues with further calls in the mock sequence and that was the start of it. For example, if I set up mocks this way I'm getting a failure trying to read the underlying Stream from the response body because there was no actual underlying http call.
Call<ResponseBody> call = mock(Call.class);
ResponseBody responseBody = mock(ResponseBody.class);
Response<ResponseBody> response = Response.success(responseBody);
when(call.execute()).thenReturn(response);
when(responseBody.string()).thenReturn("mystring"); /// Fails
java.lang.NullPointerException
at okhttp3.ResponseBody.bytes(ResponseBody.java:128)
at okhttp3.ResponseBody.string(ResponseBody.java:154)
How do I mock that out with a real Response? Unless I'm mocking every step (call, response, body read) I can't seem to get it to work.
You don't need to mock ResponseBody either. You can create instances with
its static methods.
On Mon, Nov 28, 2016 at 11:11 AM Chris Kessel notifications@github.com
wrote:
Because I ran into issues with further calls in the mock sequence and that
was the start of it. For example, if I set up mocks this way I'm getting a
failure trying to read the underlying Stream from the response body because
there was no actual underlying http call.java.lang.NullPointerException
at okhttp3.ResponseBody.bytes(ResponseBody.java:128)
at okhttp3.ResponseBody.string(ResponseBody.java:154)How do I mock that out with a real Response? Unless I'm mocking every step
(call, response, body read) I can't seem to get it to work.Call<ResponseBody> call = mock(Call.class); ResponseBody responseBody = mock(ResponseBody.class); Response<ResponseBody> response = Response.success(responseBody); when(call.execute()).thenReturn(response); when(responseBody.string()).thenReturn("mystring"); /// Fails—
You are receiving this because you modified the open/close state.Reply to this email directly, view it on GitHub
https://github.com/square/retrofit/issues/2089#issuecomment-263313410,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEEVKcbE6vzeKc14VAkryeuUMRu2dLks5rCv0ZgaJpZM4K2_y2
.
Most helpful comment
You don't need to mock ResponseBody either. You can create instances with
its static methods.
On Mon, Nov 28, 2016 at 11:11 AM Chris Kessel notifications@github.com
wrote: