我在espresso ViewAction中覆盖了perform方法,但它不起作用,该代码甚至在执行代码时也不调用

时间:2019-03-13 11:59:01

标签: java android android-espresso

我要覆盖特浓意式表演(click())以单击特定位置。我以另一种方式做到了:

public void goToCatgory (int position){

    ViewAction categoryClick = new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return null;
        }

        @Override
        public String getDescription() {
            return null;
        }

        @Override
        public void perform(UiController uiController, View view) {
            RecyclerView categoriesRecycler = (RecyclerView) view;
            SimpleBindableAdapter<Category> adapter = (SimpleBindableAdapter<Category>) categoriesRecycler.getAdapter();
            int headersCount = adapter.getHeadersCount();
            actionOnItemAtPosition(position + headersCount ,click());
        }
    };
    categoryList.perform(categoryClick);
}

执行它时,我得到NullPointerException:

java.lang.NullPointerException
at android.support.test.espresso.core.internal.deps.guava.base.Preconditions.checkNotNull(Preconditions.java:882)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:216)
at android.support.test.espresso.ViewInteraction.access$100(ViewInteraction.java:63)
at android.support.test.espresso.ViewInteraction$1.call(ViewInteraction.java:153)
at android.support.test.espresso.ViewInteraction$1.call(ViewInteraction.java:150)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6238)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

当我在调试中运行它时,我看到覆盖的perform()方法没有执行。但是我不明白我做错了什么。 谢谢。

1 个答案:

答案 0 :(得分:0)

那是因为您的ViewAction的约束返回null。在执行操作之前,它会检查其约束,该约束永远不能为null,因此请尝试在约束中返回匹配器:

@Override
public Matcher<View> getConstraints() {
    return any(View.class);
}

也许您还应该返回一个字符串来描述操作。

如果您认为单击位置正确,但是它不起作用,那是因为您没有调用正确的函数来执行:

actionOnItemAtPosition(position + headersCount ,click()).perform(uiController, view);