question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature request] Mocking for Call<Void> responses

See original GitHub issue

Given 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:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
jaredculpcommented, Apr 16, 2018

Thanks @NightlyNexus, looks like the compiler just needed a bit of help:

interface Client {
  Call<Void> request();
}

when(client.request()).thenReturn(Calls.response((Void) null));
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found