title: Testing recyclerView with Espresso, how to perform click or make assertions
body: <p>Lately I need to test RecyclerView and I am having a lot of trouble to do anything with it.</p>
  
  <p>First of all I came to know that Espresso is already "supporting" RecyclerViews by providing us RecyclerViewActions located in <a href="http://developer.android.com/reference/android/support/test/espresso/contrib/package-summary.html" rel="nofollow noreferrer">espresso-contrib</a>. So I've decided to use it and my dependencies looks like that:</p>
  
  <pre><code>dependencies {
  
      androidTestCompile 'junit:junit:4.12'
      androidTestCompile 'com.squareup.spoon:spoon-client:1.2.1'
      androidTestCompile 'com.jraska:falcon-spoon-compat:0.3.1'
      androidTestCompile 'com.android.support:support-annotations:23.1.1'
  
      androidTestCompile 'com.android.support.test:runner:0.4.1'
      androidTestCompile 'com.android.support.test:rules:0.4.1'
      androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
  
      androidTestCompile "org.mockito:mockito-core:1.10.19"
      androidTestCompile "com.google.dexmaker:dexmaker:1.2"
      androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
  }
  </code></pre>
  
  <p>But as only I try to run my test after changing espresso-core to espresso-contrib. I can see this error:</p>
  
  <pre><code>Test running failed: Instrumentation run failed due to 'java.lang.IncompatibleClassChangeError'
  </code></pre>
  
  <p>I tried to google it out. I saw many topics where people were giving responses like <a href="https://stackoverflow.com/questions/30578243/why-would-adding-espresso-contrib-cause-an-inflateexception">this one</a>. So I excluded appcompat, supportv4 and recyclerview-v7. Like that:</p>
  
  <pre><code>   androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1') {
          exclude group: 'com.android.support', module: 'appcompat'
          exclude group: 'com.android.support', module: 'support-v4'
          exclude module: 'recyclerview-v7'
      }
  </code></pre>
  
  <p>And tests start but... I can't use any RecyclerViewActions. Because there is no methods after excluding recyclerview-v7 what is recommended almost everywhere I saw on the web. When I try to use - RecyclerViewActions.actionOnItemAtPosition - the method which is core for me I get this error:</p>
  
  <pre><code>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.myapp.debug1-1/base.apk)
  at android.support.test.espresso.contrib.RecyclerViewActions$ActionOnItemAtPositionViewAction.perform(RecyclerViewActions.java:288)
  at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:135)
  at android.app.ActivityThread.main(ActivityThread.java:5254)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  </code></pre>
  
  <p>So I'd rather have this recyclerview-v7 instead of excluding it. I modify my espresso-contrib import to:</p>
  
  <pre><code> androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1') {
          exclude group: 'com.android.support', module: 'appcompat'
          exclude group: 'com.android.support', module: 'support-v4'
      }
  </code></pre>
  
  <p>Test run again but... I get error somewhere during tests:</p>
  
  <pre><code>android.view.InflateException: Binary XML file line #36: Error inflating class android.support.design.widget.NavigationView
  </code></pre>
  
  <p>So I am using most recent libs in my gradle:</p>
  
  <pre><code> compile 'com.android.support:appcompat-v7:23.1.1'
   compile 'com.android.support:support-v13:23.1.1'
   compile 'com.android.support:cardview-v7:23.1.1'
   compile 'com.android.support:design:23.1.1'
  </code></pre>
  
  <p>And NavigationView is using newest RecyclerView 23.1.1. I try to find what is wrong so I display dependencies of espresso-contrib by using ./gradlew app:dependencies in console and I can see:</p>
  
  <pre><code> +--- com.android.support.test.espresso:espresso-contrib:2.2.1
  |    +--- com.android.support.test.espresso:espresso-core:2.2.1
  |    |    +--- com.squareup:javawriter:2.1.1
  |    |    +--- com.android.support.test:runner:0.4.1 (*)
  |    |    +--- com.android.support.test:rules:0.4.1 (*)
  |    |    +--- javax.inject:javax.inject:1
  |    |    +--- org.hamcrest:hamcrest-library:1.3
  |    |    |    \--- org.hamcrest:hamcrest-core:1.3
  |    |    +--- org.hamcrest:hamcrest-integration:1.3
  |    |    |    \--- org.hamcrest:hamcrest-library:1.3 (*)
  |    |    +--- com.google.code.findbugs:jsr305:2.0.1
  |    |    +--- javax.annotation:javax.annotation-api:1.2
  |    |    \--- com.android.support.test.espresso:espresso-idling-resource:2.2.1
  |    +--- com.google.android.apps.common.testing.accessibility.framework:accessibility-test-framework:2.0
  |    |    \--- org.hamcrest:hamcrest-core:1.3
  |    \--- com.android.support:recyclerview-v7:23.0.1
  |         \--- com.android.support:support-annotations:23.0.1 -&gt; 23.1.1
  </code></pre>
  
  <p>Okay so espresso-contrib-2.2.1 which is newest one is using com.android.support:recyclerview-v7:23.0.1 - not most recent version of recyclerView and it is causing an error. I decide to update it inside espresso-contrib by adding:</p>
  
  <pre><code> androidTestCompile 'com.android.support:recyclerview-v7:23.1.1'
  </code></pre>
  
  <p>I use app:dependencies again and I can see change:</p>
  
  <pre><code>\--- com.android.support:recyclerview-v7:23.0.1 -&gt; 23.1.1 (*)
  </code></pre>
  
  <p>I start tests again. And I don't get error with NavigationView anymore - I think it's solved but... another error appears:</p>
  
  <pre><code>android.view.InflateException: Binary XML file line #21: Error inflating class android.support.v7.widget.Toolbar
  </code></pre>
  
  <p>And now I am a bit out of ideas. When you look at the dependencies of espresso-contrib it is not using anything that might use toolbar in my opinion. Toolbar is part of appcompat-v7 so I try to add it the same way as I upgraded recyclerView to newest version. So I add:</p>
  
  <pre><code>androidTestCompile 'com.android.support:appcompat-v7:23.1.1'
  </code></pre>
  
  <p>But it doesn't help me. And I am stuck.</p>
  
  <p>Did you face those problems? Do you have any workaround how to click on recyclerView? How to check if item in recyclerView has text or something like that? All of solutions I've found on the web are using method: findViewHolderForPosition which I can't use after excluding recyclerview-v7 module. Like :</p>
  
  <p><a href="https://github.com/dannyroa/espresso-samples/tree/master/RecyclerView/app/src/androidTest/java/com/dannyroa/espresso_samples/recyclerview" rel="nofollow noreferrer">https://github.com/dannyroa/espresso-samples/tree/master/RecyclerView/app/src/androidTest/java/com/dannyroa/espresso_samples/recyclerview</a></p>
  
  <p>or</p>
  
  <p><a href="https://gist.github.com/chemouna/00b10369eb1d5b00401b" rel="nofollow noreferrer">https://gist.github.com/chemouna/00b10369eb1d5b00401b</a></p>
  
  <p>or more.</p>
  
  <p> </p>