Issue
I have an app with a lot of Retrofit endpoints. I need to run this app in the emulator without internet because I do not have access anymore to the server, I am happy with fake data, so for instance if is an Int I would be happy with a random number, if is a string with whatever string.
Also I want to be able to test this app, how can I create dummy json files on the basis of the data classes in moshi, interface endpoints?
In theory on the base of all the moshi data classes, I could write some fake data, but it will take me weeks
I know there are a number of nice tools as RESTMock, but they always follow an implementation as
RESTMockServer.whenGET(RequestMatchers.pathEndsWith("/data/example.json")).thenReturnFile(
"users/example.json");
but I want to know how to automate the process, without writing a json file myself
Solution
It should be your choice of the level on which to mock. You can mock jsons if you use rest mock server, but you can go at the higher level and mock entity that actually uses your retrofit interface, or actually mock rest interface itself:
public interface RESTApiService {
@POST("user/doSomething")
Single<MyJsonResponse> userDoSomething(
@Body JsonUserDoSomething request
);
}
public class RestApiServiceImpl {
private final RESTApiService restApiService;
@Inject
public RestApiServiceImpl(RESTApiService restApiService) {
this.restApiService = restApiService;
}
public Single<MyUserDoSomethingResult> userDoSomething(User user) {
return restApiService.userDoSomething(new JsonUserDoSomething(user))
.map(jsonResponse -> jsonResponse.toMyUserDoSomethingResult());
}
}
Clearly you can pass mock version of RESTApiService into RestApiServiceImpl and let it return hand-mocked responses. Or moving same direction you could mock RestApiServiceImpl itself and thus mock not at the json models level, but entities level.
Answered By - ror
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.