android - Espresso 不会等待自定义拦截器的模拟响应
问题描述
我正在编写一个包含 Espresso 和 Retrofit 的 UI 测试。在单击下一步按钮之前,我的浓缩咖啡不会等待我的假 API 绘制所有数据。
我创建了一个这样的自定义类来进行模拟响应:
public class FakeInterceptor implements Interceptor {
private final static String TAG = FakeInterceptor.class.getSimpleName();
// FAKE JSON RESPONSES.
private final static String PROFILE__USER_RECOMMENDS = JsonDataSource.jsonUserRecommends;
private final static String PROFILE__USER = JsonDataSource.jsonUser;
@Override
public Response intercept(Chain chain) throws IOException {
Response response;
String responseString = "";
// Get Requested link.
String requestedPath = chain.request().url().encodedPath();
if (requestedPath.equalsIgnoreCase("api/users/profile/recommendsTab")) {
responseString = PROFILE__USER_RECOMMENDS;
} else if (requestedPath.equalsIgnoreCase("api/users/me")) {
responseString = PROFILE__USER;
}
response = new Response.Builder()
.code(200)
.message(responseString)
.request(chain.request())
.protocol(Protocol.HTTP_1_0)
.body(ResponseBody.create(MediaType.parse("application/json"), responseString.getBytes()))
.addHeader("content-type", "application/json")
.build();
return response;
}
}
但是,当我将改造实例FakeInterceptor
与浓缩咖啡一起使用时,就会出现问题。
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new FakeInterceptor()).build();
并构建改造实例
retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(JsonUtils.getInstance().getGson()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(Constants.END_POINT)
.client(okHttpClient)
.build();
当我使用perform(click())
for时onView
:
onView(withId(R.id.button_1)).perform(click());
onView(withId(R.id.button_2)).perform(click());
onView(withId(R.id.button_3)).perform(click());
(请记住,每个按钮都会更改片段,并且每个片段都会加载 API 以显示数据)。连续点击按钮,数据没有机会显示。
但是如果我不使用addInterceptor(new FakeInterceptor())
并让改造调用真正的API,每次点击都会等待API,允许在点击下一个按钮之前显示数据。
这就是我调用模拟 API 的方式
@Mock
UserRepository userRepository;
UserRepository mockUserRepository;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
appContext = InstrumentationRegistry.getTargetContext();
mockUserRepository = Injection.provideUserRepository(appContext); // This mockUserRepository will use the retrofit instance with `FakeInterceptor`
Mockito.when(userRepository.getUserInfo()).thenReturn(mockUserRepository.getUserInfo()); //Use the mock repository if the real user repository is called.
}
我不知道我是否遗漏了一些东西,FakeInterceptor
这会使 espresso 认为我使用的是真正的 API 而不是模拟的 API。
任何有关如何使浓缩咖啡等待 API 的帮助表示赞赏。
感谢您的时间!
更新
我放置了一个断点,如果被调用但它从未被调用responseString = PROFILE__USER
,则应该在该断点处触发。mockUserRepository.getUserInfo()
所以整个方法intercept(Chain chain)
看起来像这个 FakeInterceptor 有问题
解决方案
推荐阅读
- laravel - 如何获取在线用户及其数量?
- html - 标题偏移上方/下方的边框
- kubernetes - 如何修复 Kubernetes 上 Hyperledger Fabric 中不支持的配置类型“”错误?
- powershell - 远程执行 CMD 命令
- c++ - G4GMLParser 评估器:语法错误 C++
- python - 在 Mac OS 中用 python 解析来自特定发件人的 Outlook 邮件
- python - 简单的打印命令在 VS Code Python 扩展中不显示任何输出
- c - 用 Visual Studio 2019 编译 udis86 给了我一个未解析的外部符号
- python - 如何将 .h5 模型转换为 .tflite 并获取图像标签?
- algorithm - 像 Desmos 这样的程序使用什么算法?