I would like to simulate network communication by MockWebServer. Unfortulatelly retrofit callbacks are never invoking. My code:
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
server.play();
RestAdapter restAdapter = new RestAdapter.Builder().setConverter(new MyGsonConverter(new Gson()))
.setEndpoint(server.getUrl("/").toString()).build();
restAdapter.create(SearchService.class).getCount(StringUtils.EMPTY,
new Callback<CountContainer>() {
@Override
public void success(CountContainer countContainer, Response response) {
System.out.println("success");
}
@Override
public void failure(RetrofitError error) {
System.out.println("error");
}
});
server.shutdown();
When i use retrofit without callbacks it works.
By having a Callback
you are telling Retrofit to invoke the request and call the callback asynchronously. This means that your test is exiting before anything happens.
There are two ways to get this to work:
Executor
(one that just calls .run()
immediately) to setExecutors
on the RestAdapter.Builder
so that the background invocations and callback invocations happen synchronously.