我有一款应用程序,里面有很多Retrofit端点。我需要在没有互联网的仿真器中运行这个应用程序,因为我不能再访问服务器,我对伪造的数据很满意,因此,如果是一个Int,我会很高兴有一个随机数,如果是一个字符串与任何字符串。
另外,我希望能够测试这个应用程序,,如何根据moshi中的数据类创建虚拟json文件?接口端点?
从理论上讲,在所有moshi数据类的基础上,我可以编写一些假数据,但这将花费我数周的时间。
我知道有许多不错的工具,如RESTMock,但它们总是遵循如下实现
RESTMockServer.whenGET(RequestMatchers.pathEndsWith("/data/example.json")).thenReturnFile(
"users/example.json");但是我想知道如何自动化这个过程,而不用自己编写json文件。
发布于 2019-07-21 06:10:59
这应该是你选择的水平上的嘲弄。如果使用rest模拟服务器,您可以模拟jsons,但是可以在更高级别和模拟实体上运行,该实体实际上使用了您的rest接口,或者实际上模拟了rest接口本身:
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());
}
}显然,您可以将模拟版本的RESTApiService传递到RestApiServiceImpl中,并让它返回手动模拟的响应。或者移动相同的方向,您可以模拟RestApiServiceImpl本身,因此不是在json模型级别,而是在实体级别。
https://stackoverflow.com/questions/57005102
复制相似问题