How to test a Fragment using Espresso
<p>I have an Android fragment that I want to test. I created a test activity to which I add this fragment and run some Espresso tests.</p>

<p>However, Espresso does not find any of the views inside the fragment. It dumps the view hierarchy and it is all empty.</p>

<p>I do not want to use the actual parent activity. I want to just test this fragment in isolation. 
Has anyone done this? Is there a sample that has a similar code?</p>

<pre><code>@RunWith(AndroidJUnit4.class)
class MyFragmentTest {
    @Rule
    public ActivityTestRule activityRule = new ActivityTestRule&lt;&gt;(
    TestActivity.class);

    @Test
    public void testView() {
       MyFragment myFragment = startMyFragment();
       myFragment.onEvent(new MyEvent());
       // MyFragment has a recyclerview. 
       //OnEvent is EventBus callback that in this test contains no data.
       //I want the fragment to display empty list text and hide the recyclerView
       onView(withId(R.id.my_empty_text)).check(matches(isDisplayed()));
       onView(withId(R.id.my_recycler)).check(doesNotExist()));
    }

    private MyFragment startMyFragment() {
         FragmentActivity activity = (FragmentActivity) activityRule.getActivity();
    FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
    MyFragment myFragment = new MyFragment();
    transaction.add(myFragment, "myfrag");
    transaction.commit();
    return myFragment;
    }
}
</code></pre>