使用虚拟终结点改造值填充应用,而无需 Json 响应,而只有数据类



我有一个应用程序,其中包含许多改造端点。我需要在没有互联网的情况下在模拟器中运行此应用程序,因为我无法再访问服务器,我对虚假数据感到满意,因此例如,如果是 Int,我会对随机数感到满意,如果是带有任何字符串的字符串。

我还希望能够测试这个应用程序,如何根据 moshi 中的数据类、接口端点创建虚拟 json 文件?

理论上,在所有 moshi 数据类的基础上,我可以写一些假数据,但这需要我几周的时间

我知道有许多不错的工具作为 RESTMock,但它们总是遵循实现

RESTMockServer.whenGET(RequestMatchers.pathEndsWith("/data/example.json")).thenReturnFile(
                "users/example.json");

但我想知道如何自动化该过程,而无需自己编写 json 文件

这应该是你选择模拟的级别。如果您使用 rest 模拟服务器,您可以模拟 jsons,但您可以在更高级别和模拟实际使用您的改造接口的实体,或者实际上模拟 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模型级别,而是在实体级别进行模拟。

最新更新