首页 > 解决方案 > Recyclerview Espresso 测试导致 androidx.test.espresso.PerformException

问题描述

我有下一个需要测试的 recyclerView。

public class DashboardRecyclerViewAdapter extends RecyclerView.Adapter<DashboardRecyclerViewAdapter.DashboardViewHolder>  {
  ...typical rec view test

    //Binding the data using get() method of POJO object
    @Override
    public void onBindViewHolder(@NonNull final DashboardViewHolder holder, int position) {
        if(pad_list!=null){
            final DashboardAccountItem account  = pad_list.get(position); 
            holder.binding.accountType.setText(account.getAccountName());
               ....
        }
       .....
    }
...
    //View holder class, where all view components are defined
    public static class DashboardViewHolder extends RecyclerView.ViewHolder {
        private final DashboardAccountItemBinding binding;
        public DashboardViewHolder(DashboardAccountItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}

我想检查 recyclerview 是否有名称为“名称 99”的项目,该项目在启动时从数据库预加载:

onView(withId(R.id.accountView)).perform(RecyclerViewActions.scrollTo (hasDescendant (withText ("name 99"))));

当我运行它时,我得到了下一个错误:

    androidx.test.espresso.PerformException: Error performing 'scroll RecyclerView to: holder with view: (view is an instance of android.view.ViewGroup and has descendant matching an instance of android.widget.TextView and view.getText() with or without transformation to match: is "name 99")' on view 'view.getId() is <2131296312/com.example.recviewtestapp:id/accountView>'.
 .....
        at com.ashunevich.finobserver.RecyclerViewTest.scrollAndCheck(DashboardRecyclerViewTest.java:102) 
    ///row 102 is where test situated 
        ... 31 trimmed
    Caused by: java.lang.RuntimeException: Found 0 items matching holder with view: (view is an instance of android.view.ViewGroup and has descendant matching an instance of android.widget.TextView and view.getText() with or without transformation to match: is "name 99"), but position -1 was requested.
        at androidx.test.espresso.contrib.RecyclerViewActions$ScrollToViewAction.perform(RecyclerViewActions.java:361)
        at androidx.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:2)
        at androidx.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:22)
        at androidx.test.espresso.ViewInteraction.access$100(ViewInteraction.java:1)
        at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:2)
        at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:1)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8506)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1139)

尽管我有一个带有文本“name 99”的“accountType”项目。
将不胜感激任何帮助。

标签: androidandroid-recyclerviewerror-handlingandroid-espressocoded-ui-tests

解决方案


推荐阅读