title: Can't click on RecyclerView child in Robolectric
body: <p>I am writing tests for the view in an MVP android app. In these tests, I am using Robolectric 3.6.1. The problem I run into is getting a NullPointerException when I try to click on a view inside the RecyclerView with <code>getChildAt()</code>. This only happens when the whole test class is run, not when tests are run in isolation, which makes me think something in my setup is wrong. If I run the whole class the first test does not have this problem, but all the other ones do, so the problem seems to arise when the <code>setUp()</code> function is called more than once. </p>
  
  <p>I did some research and came across <a href="https://stackoverflow.com/questions/27052866/android-robolectric-click-recyclerview-item">this</a> post, but putting that inside my code doesn't help. </p>
  
  <pre><code>@RunWith(RobolectricTestRunner.class)
  public class MainActivityTest {
  
      private List&lt;Measurement&gt; measurements = Arrays.asList(new Measurement("Object A", Unit.CM), new Measurement("Object B", Unit.KG));
      private final int POSITION = 0;
  
      @Mock
      private MainMvpPresenter mainMvpPresenter;
  
      private MainActivity mainActivity;
  
      private RecyclerView measurementsRecyclerView;
  
      @Before
      public void setUp() throws Exception {
          MockitoAnnotations.initMocks(this);
          mainActivity = Robolectric.buildActivity(MainActivity.class).create().visible().get();
          mainActivity.setMainMvpPresenter(mainMvpPresenter);
  
          measurementsRecyclerView = mainActivity.findViewById(R.id.measurementsList);
          mainActivity.updateMeasurements(measurements);
      }
  
      @Test
      public void setUpCorrectly(){
          assertThat(measurementsRecyclerView.getChildCount(), is(measurements.size()));
      }
  
      @Test
      public void onItemClicked_ShouldCallPresenter() {
          measurementsRecyclerView.findViewHolderForAdapterPosition(POSITION).itemView.performClick();
          verify(mainMvpPresenter).measurementClicked(POSITION);
      }
  }
  </code></pre>
  
  <p>Here is the error I get:</p>
  
  <pre><code>java.lang.NullPointerException
      at com.jakkaps.body_measurements.ui.main.MainActivityTest.onItemClicked_ShouldCallPresenter(MainActivityTest.java:64)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:535)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:249)
      at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:123)
      at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:77)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131
  </code></pre>
  
  <p>)</p>