使用 anything() 时出错;在 Android 中需要测试不兼容的类型:Matcher <View> 发现:Matcher <Object>

Error when use anything(); in Android testing incompatible types required: Matcher <View> found: Matcher <Object>

我 运行 下面的代码在 return anything();

处出错
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();
        }
      }
    ;
    }

有什么想法吗?

anything() 不是泛型方法,所以你总是会得到一个 Matcher<Object>

内部 anything() 使用 IsAnything class。您可以将自己的 anyView() 方法 return 变成 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();
        }
    }
            ;
}