title: How to use findViewById() in robolectric
body: <p>I simply want to test with robolectric if a certain view is visible in a fragment.
  My unit test looks like this:</p>
  
  <pre><code>ActivityController controller = Robolectric.buildActivity(FragmentActivity.class);
  FragmentActivity activity = (FragmentActivity) controller.create().start().resume().visible().get();
  
  F fragment = new MyFragment();
  activity.getSupportFragmentManager().beginTransaction()
          .add(fragment, FRAGMENT_TAG).commit();
  
  
  View view = fragment.getView().findViewById(R.id.my_view);
  assertEquals(view.getVisibility(), View.VISIBLE);
  </code></pre>
  
  <p>I'm using the latest android gradle plugin 1.1.3, robolectirc 2.4 and robolectric gradle plugin 1.0.1 and my unit test is in the <code>test</code> folder (not <code>androidTest</code>). I can't compile that code, because the compiler can't resolve <code>R.java</code>.</p>
  
  <p>My <code>build.gradle</code>:</p>
  
  <pre><code>buildscript {
    repositories {
      jcenter()
    }
  
    dependencies {
      classpath 'com.android.tools.build:gradle:1.1.3'
      classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
      classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1'
    }
  }
  
  apply plugin: 'com.android.library'
  apply plugin: 'com.neenbedankt.android-apt'
  apply plugin: 'org.robolectric'
  
  
  android { ... }
  
  dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
  
    // Testing
    testCompile 'org.robolectric:robolectric:2.4'
    testCompile 'junit:junit:4.12'
  
    // Other dependencies
    ...
  }
  </code></pre>
  
  <p>How do I write such a unit test with robolectric?</p>
  
  <p>Update:
  Here is the complete code:</p>
  
  <pre><code>public class TestFragment extends Fragment {
  
    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      return inflater.inflate(R.layout.fragment_listview_ptr, container, false);
    }
  }
  
  
  public class MyFragmentTest {
  
    private static final String FRAGMENT_TAG = "fragment";
    private ActivityController controller;
    private FragmentActivity activity;
  
    @Test
    protected void displaysContentView(boolean pullToRefreshSupported) {
  
      controller = Robolectric.buildActivity(FragmentActivity.class);
      activity = (FragmentActivity) controller.create().start().resume().visible().get();
      Fragment fragment = new TestFragment();
  
      FragmentManager manager = activity.getSupportFragmentManager();
      manager.beginTransaction()
          .add(fragment, FRAGMENT_TAG).commit();
  
      // Compile errors here
      View loadingView = fragment.getView().findViewById(R.id.loadingView);
      View contentView = fragment.getView().findViewById(R.id.contentView);
      View errorView = fragment.getView().findViewById(R.id.loadingView);
  
      Assert.assertNotSame(loadingView.getVisibility(), View.VISIBLE);
      Assert.assertNotSame(errorView.getVisibility(), View.VISIBLE);
      Assert.assertEquals(contentView.getVisibility(), View.VISIBLE);
    }
  }
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;FrameLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center"&gt;
  
  
  &lt;android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/pull_to_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"&gt;
  
      &lt;ListView
          android:id="@+id/contentView"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:cacheColorHint="#00000000"
          android:divider="@null"
          android:fadingEdge="none"
          android:fastScrollEnabled="false"
          android:footerDividersEnabled="false"
          android:headerDividersEnabled="false"
          android:listSelector="@color/transparent"
          android:drawSelectorOnTop="true"
          android:smoothScrollbar="false"
          android:scrollbars="none"/&gt;
  &lt;/android.support.v4.widget.SwipeRefreshLayout&gt;
  
  &lt;TextView
        android:id="@+id/errorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:text="@string/error_loading_retry"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:drawableTop="@drawable/error_no_connection"
        android:drawablePadding="12dp"
        android:textColor="@color/gray_dark"
        android:padding="16dp"
        /&gt;
  
  &lt;fr.castorflex.android.circularprogressbar.CircularProgressBar
        android:id="@+id/loadingView"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:indeterminate="true"
        android:layout_gravity="center"
        android:layout_centerInParent="true"
        android:visibility="gone"
        /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>The code can be found here: <a href="https://github.com/sockeqwe/RobolectircTest">https://github.com/sockeqwe/RobolectircTest</a></p>