Espresso不等待IdlingResource

时间:2019-04-17 14:04:36

标签: java retrofit2 android-espresso

我正在使用Espresso来测试我的应用的用户界面。应用程式正在使用改良功能来传送HTTP要求到伺服器。我正在尝试使用JakeWharton/okhttp-idling-resource说服Espresso等待这些请求。但是看起来Espresso会忽略这些尝试,而无需等待就可以完成测试。

我的考试班:

@BeforeClass
public static void initClass() {
    // register idling resource for retrofit callbacks
    // dummy call for service will initialize http client
    RESTConnection.getService();
    OkHttpClient client = RESTConnection.getClient();

    idlingResource = CustomOkHttp3IdlingResource.create(
            "OkHttp", client);
    IdlingRegistry.getInstance().register(idlingResource);
}

@Test
public void idlingResourceTest() {
    ActivityTestRule<IdlingResourceTestActivity> mActivityRule = new ActivityTestRule<>(IdlingResourceTestActivity.class, true, true);
    mActivityRule.launchActivity(new Intent());

    onView(withId(R.id.text)).perform(click());
    onView(withId(R.id.text)).check(matches(withText("OK")));
}

测试活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_idling_resource_test);

    //button = findViewById(R.id.button);
    textView = findViewById(R.id.text);

    textView.setOnClickListener(new View.OnClickListener() {
                                  @Override
                                  public void onClick(View view) {
                                      test();
                                  }
                              });
}

public void test() {
    Call<Models.PlainResponse> responseCall = RESTConnection.getService().idlingResourceTest(15);
    responseCall.enqueue(new Callback<Models.PlainResponse>() {
        @Override
        public void onResponse(Call<Models.PlainResponse> call, Response<Models.PlainResponse> response) {
            textView.setText("OK");
        }

        @Override
        public void onFailure(Call<Models.PlainResponse> call, Throwable t) {

        }
    });
}

我还使用了OkHttp3IdlingResource的自定义版本,因为当前的正式版本不支持androidx。因此,我只是创建副本并更改了导入。 我是否使用空闲资源不正确?

0 个答案:

没有答案