在没有 Json 响应但只有数据 类 的情况下使用虚拟端点改造值填充应用程序
populate app with dummy endpoint retrofit values without having Json responses but only data Classes
我有一个包含很多 Retrofit 端点的应用程序。我需要在没有互联网的情况下在模拟器中 运行 这个应用程序,因为我无法再访问服务器,我对假数据很满意,所以例如如果是一个 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 并让它 return hand-mocked 响应。或者向同一个方向移动,您可以模拟 RestApiServiceImpl 本身,因此模拟的不是 json 模型级别,而是实体级别。
我有一个包含很多 Retrofit 端点的应用程序。我需要在没有互联网的情况下在模拟器中 运行 这个应用程序,因为我无法再访问服务器,我对假数据很满意,所以例如如果是一个 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 并让它 return hand-mocked 响应。或者向同一个方向移动,您可以模拟 RestApiServiceImpl 本身,因此模拟的不是 json 模型级别,而是实体级别。