title: Espresso + Junit4 - login once before running all test
body: <p>I want to write some automated test for one of my application. All of the functionality requires login.</p>
  
  <p>So, i have written test, but for each test, it is doing login and testing the functionality. Is there anyway which will help me to login only once and then run all test?</p>
  
  <p>Easiest way would be to write all test in only one test method. But i think it would be ugly way to achieve that. Any cleaner solution so, test will login only once and then run set of test.</p>
  
  <p>Following is my test code:</p>
  
  <pre><code>@RunWith(AndroidJUnit4.class)
  public class AllDisabledTest {
      public static final String USER_NAME = "all_disabled";
      public static final String DISPLAY_NAME = "All Disabled";
      public static final String PASSWORD = "1234";
  
      @Rule
      public ActivityTestRule&lt;LoginActivity&gt; mActivityRule = new ActivityTestRule&lt;&gt;(
              LoginActivity.class);
  
      @Before
      public void loginToApp(){
  
          onView(withId(R.id.edit_email)).perform(replaceText(USER_NAME));
          onView(withId(R.id.edit_password)).perform(replaceText(PASSWORD));
  
          onView(withId(R.id.login_button)).perform(click());
      }
  
      @Test
      public void checkIfFoodItemAddedToCart(){
          onData(anything()).inAdapterView(withId(R.id.menu_item_grid)).atPosition(2).perform(click());
  
          onData(anything()).inAdapterView(withId(R.id.listview)).atPosition(0).onChildView(withId(R.id.item_name)).check(matches(withText("BLUEITEM")));
      }
  }
  </code></pre>
  
  <p>Thank you in advance :).</p>