There is no virtual method findViewHolderForPosition (I) when trying to click a RecyclerView element using Espresso

I am trying to do a simple espresso test that will find the first item in a RecyclerView without a specific label and click on it. To achieve this, I added espresso-contrib to create such a project:

 androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') { // Necessary to avoid version conflicts exclude group: 'com.android.support', module: 'appcompat' exclude group: 'com.android.support', module: 'design' exclude group: 'com.android.support', module: 'support-v4' exclude group: 'com.android.support', module: 'appcompat-v7' exclude group: 'com.android.support', module: 'support-annotations' exclude module: 'recyclerview-v7' } 

and wrote the following expression in my test case: onView(withId(R.id.sresults_list_recycler)).perform(RecyclerViewActions.actionOnHolderItem(new FirstNotSoldOutMatcher(), click()).atPosition(1));

The matches work fine and the RecyclerView scrolls to the target element. But then I get the error message:

 java.lang.NoSuchMethodError: No virtual method findViewHolderForPosition(I)Landroid/support/v7/widget/RecyclerView$ViewHolder; in class Landroid/support/v7/widget/RecyclerView; or its super classes (declaration of 'android.support.v7.widget.RecyclerView' appears in /data/app/com.example-1/base.apk) at android.support.test.espresso.contrib.RecyclerViewActions$ActionOnItemAtPositionViewAction.perform(RecyclerViewActions.java:288) at android.support.test.espresso.contrib.RecyclerViewActions$ActionOnItemViewAction.perform(RecyclerViewActions.java:232) at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

It looks weird to me. I read in Google documentation that the findViewHolderForPosition method is now deprecated, but it should still be there. I also support multidex in the application, but according to the docs, it is supported out of the box, so it should not be a problem either.

Do you have any ideas what might be wrong with this test?

+5
source share
1 answer

Well, in my case, the problem was ProGuard reducing unused methods. Disabling it for testing helped.

For future seekers - if you don't want to disable proguard for debug builds, adding this line to the proguard configuration should also help:

 -keepclasseswithmembers public class android.support.v7.widget.RecyclerView { *; } 

Also note that this rule should be added to the regular proguard file (specified in proguardFiles ), and not to the test file (declared as testProguardFile )

+8
source

Source: https://habr.com/ru/post/1257458/


All Articles