[Feature request] Mocking for Call<Void> responses
See original GitHub issueGiven the following example:
public interface Example {
@GET
public Call<MyObject> getThingy();
@POST
public Call<Void> postThingy();
}
to mock the response for the GET request, I do something like:
MyObject object = new MyObject();
Call<MyObject> response = Calls.response(object);
when(example.getThingy()).thenReturn(response);
But when I try to do the same for the second request
Constructor<Void> voidConstructor = Void.class.getDeclaredConstructor();
voidConstructor.setAccessible(true);
Void voidMock = voidConstructor.newInstance();
Call<Void> response = Calls.response(voidMock);
when(example.postThingy()).thenReturn(response);
NPE is thrown (I tried some other approaches to this, but every time NPE was thrown; this was the one most likely to work) at response.enqueue
response.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
//Do something
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
//Do something
}
});
Is it possible for Call<Void> response = Calls.response(voidMock);
to return a response that will not throw a NPE? (or maybe there is another way to test the postThingy()
method; I didn’t manage to find any, since there is scarce info on how to mock Call<> responses… took me a lot of time to find what I wrote above).
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Retrofit 2 — Ignore Response Payload with Call<Void>
In most cases, the server response payload to our request is the sole reason why we made the ... Testing & Mocking; Java...
Read more >Creating Responses - Requests-Mock - Read the Docs
Responses are registered with the requests_mock.Adapter.register_uri() function on the adapter. >>> adapter.register_uri ...
Read more >Mocking Requests with Responses - Kartoza
This blog will show you an alternative to requests_mock, the one that is simpler to use yet offers more features: responses.
Read more >A testing guide for Express with request and response ...
A testing guide for Express with request and response mocking/stubbing using Jest or sinon. Curious about Advanced Jest Testing Features? Take ...
Read more >Mockito Mock Void Method - DigitalOcean
Mockito provides following methods that can be used to mock void methods. ... Mockito doAnswer() method takes Answer as argument.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks @NightlyNexus, looks like the compiler just needed a bit of help:
@jaredculp make sure you’re linking to the correct method. https://github.com/square/retrofit/blob/5c2f505a974d84763b3c2279128351b8a73a3e2d/retrofit-mock/src/main/java/retrofit2/mock/Calls.java#L37 and not https://github.com/square/retrofit/blob/5c2f505a974d84763b3c2279128351b8a73a3e2d/retrofit-mock/src/main/java/retrofit2/mock/Calls.java#L41