使用any()时出错;在Android测试中需要不兼容的类型:Matcher <view> found:Matcher <object>

时间:2015-10-21 09:13:55

标签: android testing android-testing android-espresso

我运行下面的代码并在返回任何内容时遇到错误();

error: incompatible types
required: Matcher <View>
found:    Matcher <Object>

/** 
     * Perform action of waiting until UI thread is free. <p/> E.g.: onView(isRoot()).perform(waitUntilIdle());
     * @return
     */
    public static ViewAction waitUntilIdle(){
      return new ViewAction(){
        @Override public Matcher<View> getConstraints(){
          return anything();
        }
        @Override public String getDescription(){
          return "wait until UI thread is free";
        }
        @Override public void perform(    final UiController uiController,    final View view){
          uiController.loopMainThreadUntilIdle();
        }
      }
    ;
    }

有什么想法吗?

1 个答案:

答案 0 :(得分:8)

anything()不是通用方法,因此您将始终获得Matcher<Object>

内部anything()使用IsAnything类。您可以使用自己的anyView()方法返回Matcher<View>

public static ViewAction waitUntilIdle(){
    return new ViewAction(){
        @Override public Matcher<View> getConstraints(){
            return anyView();
        }

        @NonNull
        private Matcher<View> anyView() {
            return new IsAnything<>();
        }

        @Override public String getDescription(){
            return "wait until UI thread is free";
        }
        @Override public void perform(    final UiController uiController,    final View view){
            uiController.loopMainThreadUntilIdle();
        }
    }
            ;
}